Skip to content

Commit

Permalink
SSR - copy public folder when there is no client JS (#2955)
Browse files Browse the repository at this point in the history
* SSR - copy public folder when there is no client JS

* Changest

* Use isBuildingToSSR

Co-authored-by: JuanM04 <me@juanm04.com>
  • Loading branch information
matthewp and JuanM04 authored Mar 31, 2022
1 parent e044d99 commit cebdc85
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-trainers-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix for copying public when using SSR and not client JS
26 changes: 24 additions & 2 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions packages/astro/test/fixtures/ssr-request/public/cars.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{ "name": "One" },
{ "name": "Two" },
{ "name": "Three" },
{ "name": "Four" },
{ "name": "Five" }
]
10 changes: 10 additions & 0 deletions packages/astro/test/fixtures/ssr-request/src/pages/request.astro
Original file line number Diff line number Diff line change
@@ -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 changes: 36 additions & 0 deletions packages/astro/test/ssr-request.test.js
Original file line number Diff line number Diff line change
@@ -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;
});
});

0 comments on commit cebdc85

Please sign in to comment.