0
Fork 0
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:
Matthew Phillips 2022-03-31 14:04:09 -04:00 committed by GitHub
parent e044d99696
commit cebdc85428
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix for copying public when using SSR and not client JS

View file

@ -169,17 +169,22 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
async function clientBuild(opts: StaticBuildOptions, internals: BuildInternals, input: Set<string>) {
const { astroConfig, viteConfig } = opts;
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.
if (!input.size) {
// If SSR, copy public over
if (ssr) {
await copyFiles(astroConfig.public, out);
}
return null;
}
// TODO: use vite.mergeConfig() here?
info(opts.logging, null, `\n${bgGreen(black(' building client '))}`);
const out = isBuildingToSSR(astroConfig) ? opts.buildConfig.client : astroConfig.dist;
const viteBuildConfig = {
logLevel: 'info',
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) {
info(opts.logging, 'build', 'Rearranging server assets...');
const serverRoot = opts.buildConfig.staticMode ? opts.buildConfig.client : opts.buildConfig.server;

View file

@ -0,0 +1,7 @@
[
{ "name": "One" },
{ "name": "Two" },
{ "name": "Three" },
{ "name": "Four" },
{ "name": "Five" }
]

View 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>

View 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;
});
});