fix(redirects): attempt to get only params in dev mode (#8647)
* fix(redirects): attempt to get only params in dev mode * fixtures/ssr-redirect => fixtures/redirects * add tests * Update pnpm-lock.yaml
This commit is contained in:
parent
e797b68160
commit
408b50c5ea
15 changed files with 73 additions and 11 deletions
5
.changeset/famous-numbers-smell.md
Normal file
5
.changeset/famous-numbers-smell.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fixed an issue where configured redirects with dynamic routes did not work in dev mode.
|
|
@ -1,6 +1,7 @@
|
||||||
import type { ComponentInstance, Params, Props, RouteData } from '../../@types/astro.js';
|
import type { ComponentInstance, Params, Props, RouteData } from '../../@types/astro.js';
|
||||||
import { AstroError, AstroErrorData } from '../errors/index.js';
|
import { AstroError, AstroErrorData } from '../errors/index.js';
|
||||||
import type { Logger } from '../logger/core.js';
|
import type { Logger } from '../logger/core.js';
|
||||||
|
import { routeIsRedirect } from '../redirects/index.js';
|
||||||
import { getParams } from '../routing/params.js';
|
import { getParams } from '../routing/params.js';
|
||||||
import { RouteCache, callGetStaticPaths, findPathItemByKey } from './route-cache.js';
|
import { RouteCache, callGetStaticPaths, findPathItemByKey } from './route-cache.js';
|
||||||
|
|
||||||
|
@ -25,6 +26,10 @@ export async function getParamsAndProps(opts: GetParamsAndPropsOptions): Promise
|
||||||
// This is a dynamic route, start getting the params
|
// This is a dynamic route, start getting the params
|
||||||
const params = getRouteParams(route, pathname) ?? {};
|
const params = getRouteParams(route, pathname) ?? {};
|
||||||
|
|
||||||
|
if (routeIsRedirect(route)) {
|
||||||
|
return [params, {}]
|
||||||
|
}
|
||||||
|
|
||||||
validatePrerenderEndpointCollision(route, mod, params);
|
validatePrerenderEndpointCollision(route, mod, params);
|
||||||
|
|
||||||
// During build, the route cache should already be populated.
|
// During build, the route cache should already be populated.
|
||||||
|
|
8
packages/astro/test/fixtures/redirects/src/pages/more/[dynamic].astro
vendored
Normal file
8
packages/astro/test/fixtures/redirects/src/pages/more/[dynamic].astro
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
---
|
||||||
|
export function getStaticPaths() {
|
||||||
|
return [{ params: { dynamic:'hello' } }]
|
||||||
|
}
|
||||||
|
---
|
||||||
|
{JSON.stringify(Astro.params)}
|
||||||
|
|
||||||
|
<a href="/">home</a>
|
8
packages/astro/test/fixtures/redirects/src/pages/more/[dynamic]/[route].astro
vendored
Normal file
8
packages/astro/test/fixtures/redirects/src/pages/more/[dynamic]/[route].astro
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
---
|
||||||
|
export function getStaticPaths() {
|
||||||
|
return [{ params: { dynamic:'hello', route:'world' } }]
|
||||||
|
}
|
||||||
|
---
|
||||||
|
{JSON.stringify(Astro.params)}
|
||||||
|
|
||||||
|
<a href="/">home</a>
|
8
packages/astro/test/fixtures/redirects/src/pages/more/new/[...spread].astro
vendored
Normal file
8
packages/astro/test/fixtures/redirects/src/pages/more/new/[...spread].astro
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
---
|
||||||
|
export function getStaticPaths() {
|
||||||
|
return [{ params: {spread:'welcome/world'} }]
|
||||||
|
}
|
||||||
|
---
|
||||||
|
{JSON.stringify(Astro.params)}
|
||||||
|
|
||||||
|
<a href="/">home</a>
|
|
@ -9,7 +9,7 @@ describe('Astro.redirect', () => {
|
||||||
describe('output: "server"', () => {
|
describe('output: "server"', () => {
|
||||||
before(async () => {
|
before(async () => {
|
||||||
fixture = await loadFixture({
|
fixture = await loadFixture({
|
||||||
root: './fixtures/ssr-redirect/',
|
root: './fixtures/redirects/',
|
||||||
output: 'server',
|
output: 'server',
|
||||||
adapter: testAdapter(),
|
adapter: testAdapter(),
|
||||||
redirects: {
|
redirects: {
|
||||||
|
@ -66,7 +66,7 @@ describe('Astro.redirect', () => {
|
||||||
before(async () => {
|
before(async () => {
|
||||||
process.env.STATIC_MODE = true;
|
process.env.STATIC_MODE = true;
|
||||||
fixture = await loadFixture({
|
fixture = await loadFixture({
|
||||||
root: './fixtures/ssr-redirect/',
|
root: './fixtures/redirects/',
|
||||||
output: 'static',
|
output: 'static',
|
||||||
redirects: {
|
redirects: {
|
||||||
'/old': '/test',
|
'/old': '/test',
|
||||||
|
@ -78,6 +78,9 @@ describe('Astro.redirect', () => {
|
||||||
status: 302,
|
status: 302,
|
||||||
destination: '/test',
|
destination: '/test',
|
||||||
},
|
},
|
||||||
|
'/more/old/[dynamic]': '/more/[dynamic]',
|
||||||
|
'/more/old/[dynamic]/[route]': '/more/[dynamic]/[route]',
|
||||||
|
'/more/old/[...spread]': '/more/new/[...spread]',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
await fixture.build();
|
await fixture.build();
|
||||||
|
@ -149,6 +152,12 @@ describe('Astro.redirect', () => {
|
||||||
expect(html).to.include('http-equiv="refresh');
|
expect(html).to.include('http-equiv="refresh');
|
||||||
expect(html).to.include('url=/test');
|
expect(html).to.include('url=/test');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('falls back to spread rule when dynamic rules should not match', async () => {
|
||||||
|
const html = await fixture.readFile('/more/old/welcome/world/index.html');
|
||||||
|
expect(html).to.include('http-equiv="refresh');
|
||||||
|
expect(html).to.include('url=/more/new/welcome/world');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('dev', () => {
|
describe('dev', () => {
|
||||||
|
@ -157,10 +166,13 @@ describe('Astro.redirect', () => {
|
||||||
before(async () => {
|
before(async () => {
|
||||||
process.env.STATIC_MODE = true;
|
process.env.STATIC_MODE = true;
|
||||||
fixture = await loadFixture({
|
fixture = await loadFixture({
|
||||||
root: './fixtures/ssr-redirect/',
|
root: './fixtures/redirects/',
|
||||||
output: 'static',
|
output: 'static',
|
||||||
redirects: {
|
redirects: {
|
||||||
'/one': '/',
|
'/one': '/',
|
||||||
|
'/more/old/[dynamic]': '/more/[dynamic]',
|
||||||
|
'/more/old/[dynamic]/[route]': '/more/[dynamic]/[route]',
|
||||||
|
'/more/old/[...spread]': '/more/new/[...spread]',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
devServer = await fixture.startDevServer();
|
devServer = await fixture.startDevServer();
|
||||||
|
@ -170,11 +182,27 @@ describe('Astro.redirect', () => {
|
||||||
await devServer.stop();
|
await devServer.stop();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Returns 301', async () => {
|
it('performs simple redirects', async () => {
|
||||||
let res = await fixture.fetch('/one', {
|
let res = await fixture.fetch('/one', {
|
||||||
redirect: 'manual',
|
redirect: 'manual',
|
||||||
});
|
});
|
||||||
expect(res.status).to.equal(301);
|
expect(res.status).to.equal(301);
|
||||||
|
expect(res.headers.get('Location')).to.equal('/');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('performs dynamic redirects', async () => {
|
||||||
|
const response = await fixture.fetch('/more/old/hello', { redirect: 'manual' });
|
||||||
|
expect(response.headers.get('Location')).to.equal('/more/hello');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('performs dynamic redirects with multiple params', async () => {
|
||||||
|
const response = await fixture.fetch('/more/old/hello/world', { redirect: 'manual' });
|
||||||
|
expect(response.headers.get('Location')).to.equal('/more/hello/world');
|
||||||
|
});
|
||||||
|
|
||||||
|
it.skip('falls back to spread rule when dynamic rules should not match', async () => {
|
||||||
|
const response = await fixture.fetch('/more/old/welcome/world', { redirect: 'manual' });
|
||||||
|
expect(response.headers.get('Location')).to.equal('/more/new/welcome/world');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -183,7 +211,7 @@ describe('Astro.redirect', () => {
|
||||||
before(async () => {
|
before(async () => {
|
||||||
process.env.STATIC_MODE = true;
|
process.env.STATIC_MODE = true;
|
||||||
fixture = await loadFixture({
|
fixture = await loadFixture({
|
||||||
root: './fixtures/ssr-redirect/',
|
root: './fixtures/redirects/',
|
||||||
output: 'static',
|
output: 'static',
|
||||||
redirects: {
|
redirects: {
|
||||||
'/one': '/',
|
'/one': '/',
|
||||||
|
|
|
@ -3054,6 +3054,12 @@ importers:
|
||||||
specifier: workspace:*
|
specifier: workspace:*
|
||||||
version: link:../../..
|
version: link:../../..
|
||||||
|
|
||||||
|
packages/astro/test/fixtures/redirects:
|
||||||
|
dependencies:
|
||||||
|
astro:
|
||||||
|
specifier: workspace:*
|
||||||
|
version: link:../../..
|
||||||
|
|
||||||
packages/astro/test/fixtures/reexport-astro-containing-client-component:
|
packages/astro/test/fixtures/reexport-astro-containing-client-component:
|
||||||
dependencies:
|
dependencies:
|
||||||
'@astrojs/preact':
|
'@astrojs/preact':
|
||||||
|
@ -3339,12 +3345,6 @@ importers:
|
||||||
specifier: workspace:*
|
specifier: workspace:*
|
||||||
version: link:../../..
|
version: link:../../..
|
||||||
|
|
||||||
packages/astro/test/fixtures/ssr-redirect:
|
|
||||||
dependencies:
|
|
||||||
astro:
|
|
||||||
specifier: workspace:*
|
|
||||||
version: link:../../..
|
|
||||||
|
|
||||||
packages/astro/test/fixtures/ssr-request:
|
packages/astro/test/fixtures/ssr-request:
|
||||||
dependencies:
|
dependencies:
|
||||||
astro:
|
astro:
|
||||||
|
|
Loading…
Reference in a new issue