Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/moody-lines-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue where `Astro.originPathname` wasn't returning the correct value when using rewrites.
3 changes: 3 additions & 0 deletions packages/astro/src/core/middleware/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AstroError } from '../errors/index.js';
import { apiContextRoutesSymbol } from '../render-context.js';
import { type Pipeline, getParams } from '../render/index.js';
import { defineMiddleware } from './index.js';
import { setOriginPathname } from '../routing/rewrite.js';

// From SvelteKit: https://github.com/sveltejs/kit/blob/master/packages/kit/src/exports/hooks/sequence.js
/**
Expand Down Expand Up @@ -46,6 +47,7 @@ export function sequence(...handlers: MiddlewareHandler[]): MiddlewareHandler {
handleContext.request,
);
}
const oldPathname = handleContext.url.pathname;
const pipeline: Pipeline = Reflect.get(handleContext, apiContextRoutesSymbol);
const { routeData, pathname } = await pipeline.tryRewrite(
payload,
Expand Down Expand Up @@ -76,6 +78,7 @@ export function sequence(...handlers: MiddlewareHandler[]): MiddlewareHandler {
handleContext.url = new URL(newRequest.url);
handleContext.cookies = new AstroCookies(newRequest);
handleContext.params = getParams(routeData, pathname);
setOriginPathname(handleContext.request, oldPathname);
}
return applyHandle(i + 1, handleContext);
} else {
Expand Down
4 changes: 4 additions & 0 deletions packages/astro/src/core/render-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export class RenderContext {
}
const lastNext = async (ctx: APIContext, payload?: RewritePayload) => {
if (payload) {
const oldPathname = this.pathname;
pipeline.logger.debug('router', 'Called rewriting to:', payload);
// we intentionally let the error bubble up
const {
Expand Down Expand Up @@ -197,6 +198,7 @@ export class RenderContext {
this.params = getParams(routeData, pathname);
this.pathname = pathname;
this.status = 200;
setOriginPathname(this.request, oldPathname);
}
let response: Response;

Expand Down Expand Up @@ -296,6 +298,7 @@ export class RenderContext {

async #executeRewrite(reroutePayload: RewritePayload) {
this.pipeline.logger.debug('router', 'Calling rewrite: ', reroutePayload);
const oldPathname = this.pathname;
const { routeData, componentInstance, newUrl, pathname } = await this.pipeline.tryRewrite(
reroutePayload,
this.request,
Expand Down Expand Up @@ -331,6 +334,7 @@ export class RenderContext {
this.isRewriting = true;
// we found a route and a component, we can change the status code to 200
this.status = 200;
setOriginPathname(this.request, oldPathname);
return await this.render(componentInstance);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/astro/test/fixtures/reroute/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
const auth = Astro.locals.auth;
const origin = Astro.originPathname;
---
<html>
<head>
Expand All @@ -8,5 +9,6 @@ const auth = Astro.locals.auth;
<body>
<h1>Index</h1>
{auth ? <p>Called auth</p>: ""}
<h2>Origin: {origin}</h2>
</body>
</html>
1 change: 1 addition & 0 deletions packages/astro/test/rewrite.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('Dev reroute', () => {
const $ = cheerioLoad(html);

assert.equal($('h1').text(), 'Index');
assert.equal($('h2').text(), 'Origin: /reroute');
});

it('should render the index page when navigating /blog/hello ', async () => {
Expand Down