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 <nate@astro.build> Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
This commit is contained in:
parent
144813f730
commit
409c60028a
3 changed files with 49 additions and 1 deletions
5
.changeset/chatty-rats-whisper.md
Normal file
5
.changeset/chatty-rats-whisper.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/node': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Support directory redirects and query params at the same time
|
|
@ -57,7 +57,14 @@ export function createServer(
|
||||||
});
|
});
|
||||||
stream.on('directory', () => {
|
stream.on('directory', () => {
|
||||||
// On directory find, redirect to the trailing slash
|
// 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.statusCode = 301;
|
||||||
res.setHeader('Location', location);
|
res.setHeader('Location', location);
|
||||||
res.end(location);
|
res.end(location);
|
||||||
|
|
|
@ -58,6 +58,15 @@ describe('Prerendering', () => {
|
||||||
expect($('h1').text()).to.equal('Two');
|
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 () => {
|
it('Can render prerendered route with query params', async () => {
|
||||||
const res = await fetch(`http://${server.host}:${server.port}/some-base/two/?foo=bar`);
|
const res = await fetch(`http://${server.host}:${server.port}/some-base/two/?foo=bar`);
|
||||||
const html = await res.text();
|
const html = await res.text();
|
||||||
|
@ -116,6 +125,15 @@ describe('Prerendering', () => {
|
||||||
expect($('h1').text()).to.equal('Two');
|
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 () => {
|
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}/two/?foo=bar`);
|
||||||
const html = await res.text();
|
const html = await res.text();
|
||||||
|
@ -171,6 +189,15 @@ describe('Hybrid rendering', () => {
|
||||||
expect($('h1').text()).to.equal('One');
|
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 () => {
|
it('Can render prerendered route with query params', async () => {
|
||||||
const res = await fetch(`http://${server.host}:${server.port}/some-base/one/?foo=bar`);
|
const res = await fetch(`http://${server.host}:${server.port}/some-base/one/?foo=bar`);
|
||||||
const html = await res.text();
|
const html = await res.text();
|
||||||
|
@ -228,6 +255,15 @@ describe('Hybrid rendering', () => {
|
||||||
expect($('h1').text()).to.equal('One');
|
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 () => {
|
it('Can render prerendered route with query params', async () => {
|
||||||
const res = await fetch(`http://${server.host}:${server.port}/one/?foo=bar`);
|
const res = await fetch(`http://${server.host}:${server.port}/one/?foo=bar`);
|
||||||
const html = await res.text();
|
const html = await res.text();
|
||||||
|
|
Loading…
Reference in a new issue