From 409c60028aaab09b8f2383ef5730531cd23db4ba Mon Sep 17 00:00:00 2001 From: Riki Date: Tue, 6 Jun 2023 23:09:00 +0800 Subject: [PATCH] fix:query not considered in directory redirection (#7243) * fix:query not considered in directory redirection * feat: req.url may be empty * test(node): add redirect + query param tests * refactor(node): cleanup query param logic * chore: remove log * chore: add changeset --------- Co-authored-by: Riki <947968273@qq.com> Co-authored-by: Nate Moore Co-authored-by: Nate Moore --- .changeset/chatty-rats-whisper.md | 5 +++ packages/integrations/node/src/http-server.ts | 9 ++++- .../integrations/node/test/prerender.test.js | 36 +++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 .changeset/chatty-rats-whisper.md diff --git a/.changeset/chatty-rats-whisper.md b/.changeset/chatty-rats-whisper.md new file mode 100644 index 000000000..42df7ea8c --- /dev/null +++ b/.changeset/chatty-rats-whisper.md @@ -0,0 +1,5 @@ +--- +'@astrojs/node': patch +--- + +Support directory redirects and query params at the same time diff --git a/packages/integrations/node/src/http-server.ts b/packages/integrations/node/src/http-server.ts index 177c71ed9..9f8b3e891 100644 --- a/packages/integrations/node/src/http-server.ts +++ b/packages/integrations/node/src/http-server.ts @@ -57,7 +57,14 @@ export function createServer( }); stream.on('directory', () => { // On directory find, redirect to the trailing slash - const location = req.url + '/'; + let location: string; + if (req.url!.includes('?')) { + const [url = '', search] = req.url!.split('?'); + location = `${url}/?${search}` + } else { + location = req.url + '/' + } + res.statusCode = 301; res.setHeader('Location', location); res.end(location); diff --git a/packages/integrations/node/test/prerender.test.js b/packages/integrations/node/test/prerender.test.js index 4b5522a31..a018f0649 100644 --- a/packages/integrations/node/test/prerender.test.js +++ b/packages/integrations/node/test/prerender.test.js @@ -58,6 +58,15 @@ describe('Prerendering', () => { expect($('h1').text()).to.equal('Two'); }); + it('Can render prerendered route with redirect and query params', async () => { + 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('Can render prerendered route with query params', async () => { const res = await fetch(`http://${server.host}:${server.port}/some-base/two/?foo=bar`); const html = await res.text(); @@ -116,6 +125,15 @@ describe('Prerendering', () => { expect($('h1').text()).to.equal('Two'); }); + it('Can render prerendered route with redirect and query params', async () => { + const res = await fetch(`http://${server.host}:${server.port}/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('Can render prerendered route with query params', async () => { const res = await fetch(`http://${server.host}:${server.port}/two/?foo=bar`); const html = await res.text(); @@ -171,6 +189,15 @@ describe('Hybrid rendering', () => { expect($('h1').text()).to.equal('One'); }); + it('Can render prerendered route with redirect and query params', async () => { + const res = await fetch(`http://${server.host}:${server.port}/some-base/one?foo=bar`); + const html = await res.text(); + const $ = cheerio.load(html); + + expect(res.status).to.equal(200); + expect($('h1').text()).to.equal('One'); + }); + it('Can render prerendered route with query params', async () => { const res = await fetch(`http://${server.host}:${server.port}/some-base/one/?foo=bar`); const html = await res.text(); @@ -228,6 +255,15 @@ describe('Hybrid rendering', () => { expect($('h1').text()).to.equal('One'); }); + it('Can render prerendered route with redirect and query params', async () => { + const res = await fetch(`http://${server.host}:${server.port}/one?foo=bar`); + const html = await res.text(); + const $ = cheerio.load(html); + + expect(res.status).to.equal(200); + expect($('h1').text()).to.equal('One'); + }); + it('Can render prerendered route with query params', async () => { const res = await fetch(`http://${server.host}:${server.port}/one/?foo=bar`); const html = await res.text();