Skip to content

Commit ec5ca59

Browse files
HiDeoodelucis
andauthored
Fix issue with rewriting (#2904)
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
1 parent 070ec8b commit ec5ca59

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

.changeset/forty-ways-beam.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/starlight': patch
3+
---
4+
5+
Fixes an issue preventing the use of [rewrites](https://docs.astro.build/en/guides/routing/#rewrites).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineMiddleware } from 'astro:middleware';
2+
3+
export const onRequest = defineMiddleware((context, next) => {
4+
if (context.url.pathname === '/content') {
5+
return context.rewrite('/demo');
6+
}
7+
return next();
8+
});

packages/starlight/__e2e__/ssr.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ test('SSR mode renders the same splash page as prerendering', async ({
5454
expectEquivalentHTML(prerenderContent, ssrContent);
5555
});
5656

57+
test('supports middleware rewriting', async ({ page, getProdServer }) => {
58+
const starlight = await getProdServer();
59+
const response = await starlight.goto('/content');
60+
61+
expect(response?.status()).toBe(200);
62+
await expect(page.locator('#server-check')).toHaveText('On server');
63+
});
64+
5765
function expectEquivalentHTML(a: string, b: string) {
5866
expect(getNormalizedHTML(a)).toEqual(getNormalizedHTML(b));
5967
}

packages/starlight/locals.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const onRequest = defineMiddleware(async (context, next) => {
2020
* avoid generating route data in this middleware which also runs for non-Starlight route.
2121
*/
2222
export function initializeStarlightRoute(context: APIContext) {
23+
if ('starlightRoute' in context.locals) return;
2324
const state: { routeData: StarlightRouteData | undefined } = { routeData: undefined };
2425
Object.defineProperty(context.locals, 'starlightRoute', {
2526
get() {

0 commit comments

Comments
 (0)