mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
SSR - copy public folder when there is no client JS (#2955)
* SSR - copy public folder when there is no client JS * Changest * Use isBuildingToSSR Co-authored-by: JuanM04 <me@juanm04.com>
This commit is contained in:
parent
e044d99696
commit
cebdc85428
5 changed files with 82 additions and 2 deletions
5
.changeset/swift-trainers-suffer.md
Normal file
5
.changeset/swift-trainers-suffer.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix for copying public when using SSR and not client JS
|
|
@ -169,17 +169,22 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
|
||||||
async function clientBuild(opts: StaticBuildOptions, internals: BuildInternals, input: Set<string>) {
|
async function clientBuild(opts: StaticBuildOptions, internals: BuildInternals, input: Set<string>) {
|
||||||
const { astroConfig, viteConfig } = opts;
|
const { astroConfig, viteConfig } = opts;
|
||||||
const timer = performance.now();
|
const timer = performance.now();
|
||||||
|
const ssr = isBuildingToSSR(astroConfig);
|
||||||
|
const out = ssr ? opts.buildConfig.client : astroConfig.dist;
|
||||||
|
|
||||||
// Nothing to do if there is no client-side JS.
|
// Nothing to do if there is no client-side JS.
|
||||||
if (!input.size) {
|
if (!input.size) {
|
||||||
|
// If SSR, copy public over
|
||||||
|
if (ssr) {
|
||||||
|
await copyFiles(astroConfig.public, out);
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: use vite.mergeConfig() here?
|
// TODO: use vite.mergeConfig() here?
|
||||||
info(opts.logging, null, `\n${bgGreen(black(' building client '))}`);
|
info(opts.logging, null, `\n${bgGreen(black(' building client '))}`);
|
||||||
|
|
||||||
const out = isBuildingToSSR(astroConfig) ? opts.buildConfig.client : astroConfig.dist;
|
|
||||||
|
|
||||||
const viteBuildConfig = {
|
const viteBuildConfig = {
|
||||||
logLevel: 'info',
|
logLevel: 'info',
|
||||||
mode: 'production',
|
mode: 'production',
|
||||||
|
@ -236,6 +241,23 @@ async function cleanSsrOutput(opts: StaticBuildOptions) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function copyFiles(fromFolder: URL, toFolder: URL) {
|
||||||
|
const files = await glob('**/*', {
|
||||||
|
cwd: fileURLToPath(fromFolder),
|
||||||
|
});
|
||||||
|
|
||||||
|
// Make the directory
|
||||||
|
await fs.promises.mkdir(toFolder, { recursive: true });
|
||||||
|
|
||||||
|
await Promise.all(
|
||||||
|
files.map(async (filename) => {
|
||||||
|
const from = new URL(filename, fromFolder);
|
||||||
|
const to = new URL(filename, toFolder);
|
||||||
|
return fs.promises.copyFile(from, to);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
async function ssrMoveAssets(opts: StaticBuildOptions) {
|
async function ssrMoveAssets(opts: StaticBuildOptions) {
|
||||||
info(opts.logging, 'build', 'Rearranging server assets...');
|
info(opts.logging, 'build', 'Rearranging server assets...');
|
||||||
const serverRoot = opts.buildConfig.staticMode ? opts.buildConfig.client : opts.buildConfig.server;
|
const serverRoot = opts.buildConfig.staticMode ? opts.buildConfig.client : opts.buildConfig.server;
|
||||||
|
|
7
packages/astro/test/fixtures/ssr-request/public/cars.json
vendored
Normal file
7
packages/astro/test/fixtures/ssr-request/public/cars.json
vendored
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
[
|
||||||
|
{ "name": "One" },
|
||||||
|
{ "name": "Two" },
|
||||||
|
{ "name": "Three" },
|
||||||
|
{ "name": "Four" },
|
||||||
|
{ "name": "Five" }
|
||||||
|
]
|
10
packages/astro/test/fixtures/ssr-request/src/pages/request.astro
vendored
Normal file
10
packages/astro/test/fixtures/ssr-request/src/pages/request.astro
vendored
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
---
|
||||||
|
const origin = new URL(Astro.request.url).origin;
|
||||||
|
---
|
||||||
|
|
||||||
|
<html>
|
||||||
|
<head><title>Testing</title></head>
|
||||||
|
<body>
|
||||||
|
<h1 id="origin">{origin}</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
36
packages/astro/test/ssr-request.test.js
Normal file
36
packages/astro/test/ssr-request.test.js
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
|
||||||
|
import { expect } from 'chai';
|
||||||
|
import { load as cheerioLoad } from 'cheerio';
|
||||||
|
import { loadFixture } from './test-utils.js';
|
||||||
|
import testAdapter from './test-adapter.js';
|
||||||
|
|
||||||
|
// Asset bundling
|
||||||
|
describe('Using Astro.request in SSR', () => {
|
||||||
|
/** @type {import('./test-utils').Fixture} */
|
||||||
|
let fixture;
|
||||||
|
|
||||||
|
before(async () => {
|
||||||
|
fixture = await loadFixture({
|
||||||
|
projectRoot: './fixtures/ssr-request/',
|
||||||
|
buildOptions: {
|
||||||
|
experimentalSsr: true,
|
||||||
|
},
|
||||||
|
adapter: testAdapter(),
|
||||||
|
});
|
||||||
|
await fixture.build();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Gets the request pased in', async () => {
|
||||||
|
const app = await fixture.loadTestAdapterApp();
|
||||||
|
const request = new Request('http://example.com/request');
|
||||||
|
const response = await app.render(request);
|
||||||
|
const html = await response.text();
|
||||||
|
const $ = cheerioLoad(html);
|
||||||
|
expect($('#origin').text()).to.equal('http://example.com');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('public file is copied over', async () => {
|
||||||
|
const json = await fixture.readFile('/client/cars.json');
|
||||||
|
expect(json).to.not.be.undefined;
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue