From 781f558c401a5f02927d150e4628a77c55cccd28 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Fri, 12 May 2023 10:01:05 -0400 Subject: [PATCH] Correct handle directory finds when using base in the Node adapter (#7076) --- .changeset/afraid-suits-talk.md | 5 +++++ packages/integrations/node/src/http-server.ts | 8 +++++++- packages/integrations/node/test/prerender.test.js | 15 ++++++++++++--- 3 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 .changeset/afraid-suits-talk.md diff --git a/.changeset/afraid-suits-talk.md b/.changeset/afraid-suits-talk.md new file mode 100644 index 000000000..1177bf0ed --- /dev/null +++ b/.changeset/afraid-suits-talk.md @@ -0,0 +1,5 @@ +--- +'@astrojs/node': patch +--- + +Fix redirects on directories when using base option diff --git a/packages/integrations/node/src/http-server.ts b/packages/integrations/node/src/http-server.ts index 850d61bbb..2c58c0da7 100644 --- a/packages/integrations/node/src/http-server.ts +++ b/packages/integrations/node/src/http-server.ts @@ -55,7 +55,13 @@ export function createServer( // File not found, forward to the SSR handler handler(req, res); }); - + stream.on('directory', () => { + // On directory find, redirect to the trailing slash + const location = req.url + '/'; + res.statusCode = 301 + res.setHeader('Location', location); + res.end(location); + }); stream.on('file', () => { forwardError = true; }); diff --git a/packages/integrations/node/test/prerender.test.js b/packages/integrations/node/test/prerender.test.js index e5c94391f..1b478bb33 100644 --- a/packages/integrations/node/test/prerender.test.js +++ b/packages/integrations/node/test/prerender.test.js @@ -12,6 +12,7 @@ describe('Prerendering', () => { before(async () => { process.env.ASTRO_NODE_AUTOSTART = 'disabled'; fixture = await loadFixture({ + base: '/some-base', root: './fixtures/prerender/', output: 'server', adapter: nodejs({ mode: 'standalone' }), @@ -32,7 +33,7 @@ describe('Prerendering', () => { } it('Can render SSR route', async () => { - const res = await fetch(`http://${server.host}:${server.port}/one`); + const res = await fetch(`http://${server.host}:${server.port}/some-base/one`); const html = await res.text(); const $ = cheerio.load(html); @@ -41,7 +42,7 @@ describe('Prerendering', () => { }); it('Can render prerendered route', async () => { - const res = await fetch(`http://${server.host}:${server.port}/two`); + const res = await fetch(`http://${server.host}:${server.port}/some-base/two`); const html = await res.text(); const $ = cheerio.load(html); @@ -50,11 +51,19 @@ describe('Prerendering', () => { }); it('Can render prerendered route with query params', async () => { - const res = await fetch(`http://${server.host}:${server.port}/two?foo=bar`); + const res = await fetch(`http://${server.host}:${server.port}/some-base/two/?foo=bar`); const html = await res.text(); const $ = cheerio.load(html); expect(res.status).to.equal(200); expect($('h1').text()).to.equal('Two'); }); + + it('Omitting the trailing slash results in a redirect that includes the base', async () => { + const res = await fetch(`http://${server.host}:${server.port}/some-base/two`, { + redirect: 'manual' + }); + expect(res.status).to.equal(301); + expect(res.headers.get('location')).to.equal('/some-base/two/'); + }); });