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

Add support for serving well-known URIs with the @astrojs/node SSR adapter #5832

Merged
merged 2 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .changeset/two-needles-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': minor
'@astrojs/node': minor
---

Add support for serving well-known URIs with the @astrojs/node SSR adapter
5 changes: 3 additions & 2 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ async function clientBuild(
if (!input.size) {
// If SSR, copy public over
if (ssr) {
await copyFiles(settings.config.publicDir, out);
await copyFiles(settings.config.publicDir, out, true);
}

return null;
Expand Down Expand Up @@ -318,9 +318,10 @@ async function cleanServerOutput(opts: StaticBuildOptions) {
}
}

async function copyFiles(fromFolder: URL, toFolder: URL) {
async function copyFiles(fromFolder: URL, toFolder: URL, includeDotfiles = false) {
const files = await glob('**/*', {
cwd: fileURLToPath(fromFolder),
dot: includeDotfiles,
});

await Promise.all(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"applinks": {}
}
4 changes: 3 additions & 1 deletion packages/astro/test/static-build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ describe('Static build SSR', () => {
root: './fixtures/static-build-ssr/',
});
await fixture.build();
const asset = await fixture.readFile('/client/nested/asset2.txt');

await fixture.readFile('/client/nested/asset2.txt');
await fixture.readFile('/client/.well-known/apple-app-site-association');
});
});
5 changes: 3 additions & 2 deletions packages/integrations/node/src/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ export function createServer(
) {
const listener: http.RequestListener = (req, res) => {
if (req.url) {
const pathname = '/' + removeBase(req.url);
let pathname = removeBase(req.url);
pathname = pathname[0] === '/' ? pathname : '/' + pathname;
const stream = send(req, encodeURI(pathname), {
root: fileURLToPath(client),
dotfiles: 'deny',
dotfiles: pathname.startsWith('/.well-known/') ? 'allow' : 'deny',
});

let forwardError = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/well-known-locations",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/node": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"applinks": {}
}
45 changes: 45 additions & 0 deletions packages/integrations/node/test/well-known-locations.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import nodejs from '../dist/index.js';
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';

describe('test URIs beginning with a dot', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/well-known-locations/',
output: 'server',
adapter: nodejs({ mode: 'standalone' }),
});
await fixture.build();
});

describe('can load well-known URIs', async () => {
let devPreview;

before(async () => {
devPreview = await fixture.preview();
});

after(async () => {
await devPreview.stop();
});

it('can load a valid well-known URI', async () => {
const res = await fixture.fetch('/.well-known/apple-app-site-association');

expect(res.status).to.equal(200);

const json = await res.json();

expect(json).to.deep.equal({ applinks: {} });
});

it('cannot load a dot folder that is not a well-known URI', async () => {
const res = await fixture.fetch('/.hidden/file.json');

expect(res.status).to.equal(404);
});
});
});
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.