Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSR - copy public folder when there is no client JS #2955

Merged
merged 3 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
23 changes: 23 additions & 0 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ async function clientBuild(opts: StaticBuildOptions, internals: BuildInternals,

// Nothing to do if there is no client-side JS.
if (!input.size) {
const ssr = !!astroConfig._ctx.adapter?.serverEntrypoint;
// If SSR, copy public over
if(ssr) {
await copyFiles(astroConfig.public, opts.buildConfig.client);
}

return null;
}

Expand Down Expand Up @@ -236,6 +242,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;
});
});