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

fix:query not considered in directory redirection #7243

Merged
merged 8 commits into from
Jun 6, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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