Skip to content

Commit

Permalink
fix:query not considered in directory redirection (#7243)
Browse files Browse the repository at this point in the history
* 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 <nate@astro.build>
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
  • Loading branch information
4 people authored Jun 6, 2023
1 parent 144813f commit 409c600
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/chatty-rats-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/node': patch
---

Support directory redirects and query params at the same time
9 changes: 8 additions & 1 deletion packages/integrations/node/src/http-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 36 additions & 0 deletions packages/integrations/node/test/prerender.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 409c600

Please sign in to comment.