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:
Arsh 2023-09-27 22:19:06 +00:00 committed by GitHub
parent e797b68160
commit 408b50c5ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 73 additions and 11 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixed an issue where configured redirects with dynamic routes did not work in dev mode.

View file

@ -1,6 +1,7 @@
import type { ComponentInstance, Params, Props, RouteData } from '../../@types/astro.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
import type { Logger } from '../logger/core.js';
import { routeIsRedirect } from '../redirects/index.js';
import { getParams } from '../routing/params.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
const params = getRouteParams(route, pathname) ?? {};
if (routeIsRedirect(route)) {
return [params, {}]
}
validatePrerenderEndpointCollision(route, mod, params);
// During build, the route cache should already be populated.

View file

@ -0,0 +1,8 @@
---
export function getStaticPaths() {
return [{ params: { dynamic:'hello' } }]
}
---
{JSON.stringify(Astro.params)}
<a href="/">home</a>

View file

@ -0,0 +1,8 @@
---
export function getStaticPaths() {
return [{ params: { dynamic:'hello', route:'world' } }]
}
---
{JSON.stringify(Astro.params)}
<a href="/">home</a>

View file

@ -0,0 +1,8 @@
---
export function getStaticPaths() {
return [{ params: {spread:'welcome/world'} }]
}
---
{JSON.stringify(Astro.params)}
<a href="/">home</a>

View file

@ -9,7 +9,7 @@ describe('Astro.redirect', () => {
describe('output: "server"', () => {
before(async () => {
fixture = await loadFixture({
root: './fixtures/ssr-redirect/',
root: './fixtures/redirects/',
output: 'server',
adapter: testAdapter(),
redirects: {
@ -66,7 +66,7 @@ describe('Astro.redirect', () => {
before(async () => {
process.env.STATIC_MODE = true;
fixture = await loadFixture({
root: './fixtures/ssr-redirect/',
root: './fixtures/redirects/',
output: 'static',
redirects: {
'/old': '/test',
@ -78,6 +78,9 @@ describe('Astro.redirect', () => {
status: 302,
destination: '/test',
},
'/more/old/[dynamic]': '/more/[dynamic]',
'/more/old/[dynamic]/[route]': '/more/[dynamic]/[route]',
'/more/old/[...spread]': '/more/new/[...spread]',
},
});
await fixture.build();
@ -149,6 +152,12 @@ describe('Astro.redirect', () => {
expect(html).to.include('http-equiv="refresh');
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', () => {
@ -157,10 +166,13 @@ describe('Astro.redirect', () => {
before(async () => {
process.env.STATIC_MODE = true;
fixture = await loadFixture({
root: './fixtures/ssr-redirect/',
root: './fixtures/redirects/',
output: 'static',
redirects: {
'/one': '/',
'/more/old/[dynamic]': '/more/[dynamic]',
'/more/old/[dynamic]/[route]': '/more/[dynamic]/[route]',
'/more/old/[...spread]': '/more/new/[...spread]',
},
});
devServer = await fixture.startDevServer();
@ -170,11 +182,27 @@ describe('Astro.redirect', () => {
await devServer.stop();
});
it('Returns 301', async () => {
it('performs simple redirects', async () => {
let res = await fixture.fetch('/one', {
redirect: 'manual',
});
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 () => {
process.env.STATIC_MODE = true;
fixture = await loadFixture({
root: './fixtures/ssr-redirect/',
root: './fixtures/redirects/',
output: 'static',
redirects: {
'/one': '/',

View file

@ -3054,6 +3054,12 @@ importers:
specifier: workspace:*
version: link:../../..
packages/astro/test/fixtures/redirects:
dependencies:
astro:
specifier: workspace:*
version: link:../../..
packages/astro/test/fixtures/reexport-astro-containing-client-component:
dependencies:
'@astrojs/preact':
@ -3339,12 +3345,6 @@ importers:
specifier: workspace:*
version: link:../../..
packages/astro/test/fixtures/ssr-redirect:
dependencies:
astro:
specifier: workspace:*
version: link:../../..
packages/astro/test/fixtures/ssr-request:
dependencies:
astro: