fix: cookies in redirect

This commit is contained in:
Emanuele Stoppa 2023-08-23 11:41:24 +01:00
parent 353dcb23e8
commit 25f448b68e
5 changed files with 19 additions and 4 deletions

View file

@ -48,12 +48,14 @@ export function createAPIContext({
generator: `Astro v${ASTRO_VERSION}`, generator: `Astro v${ASTRO_VERSION}`,
props, props,
redirect(path, status) { redirect(path, status) {
return new Response(null, { const response = new Response(null, {
status: status || 302, status: status || 302,
headers: { headers: {
Location: path, Location: path,
}, },
}); });
attachCookiesToResponse(response, context.cookies);
return response;
}, },
ResponseWithEncoding, ResponseWithEncoding,
url: new URL(request.url), url: new URL(request.url),

View file

@ -24,12 +24,14 @@ export type RenderPage = {
export async function renderPage({ mod, renderContext, env, cookies }: RenderPage) { export async function renderPage({ mod, renderContext, env, cookies }: RenderPage) {
if (routeIsRedirect(renderContext.route)) { if (routeIsRedirect(renderContext.route)) {
return new Response(null, { const response = new Response(null, {
status: redirectRouteStatus(renderContext.route, renderContext.request.method), status: redirectRouteStatus(renderContext.route, renderContext.request.method),
headers: { headers: {
location: redirectRouteGenerate(renderContext.route, renderContext.params), location: redirectRouteGenerate(renderContext.route, renderContext.params),
}, },
}); });
attachToResponse(response, cookies);
return response;
} }
// Validate the page component before rendering the page // Validate the page component before rendering the page

View file

@ -18,14 +18,14 @@ const first = defineMiddleware(async (context, next) => {
return new Response(JSON.stringify(object), { return new Response(JSON.stringify(object), {
headers: response.headers, headers: response.headers,
}); });
} else if(context.url.pathname === '/clone') { } else if (context.url.pathname === '/clone') {
const response = await next(); const response = await next();
const newResponse = response.clone(); const newResponse = response.clone();
const /** @type {string} */ html = await newResponse.text(); const /** @type {string} */ html = await newResponse.text();
const newhtml = html.replace('<h1>testing</h1>', '<h1>it works</h1>'); const newhtml = html.replace('<h1>testing</h1>', '<h1>it works</h1>');
return new Response(newhtml, { status: 200, headers: response.headers }); return new Response(newhtml, { status: 200, headers: response.headers });
} else { } else {
if(context.url.pathname === '/') { if (context.url.pathname === '/') {
context.cookies.set('foo', 'bar'); context.cookies.set('foo', 'bar');
} }
@ -38,6 +38,7 @@ const second = defineMiddleware(async (context, next) => {
if (context.request.url.includes('/second')) { if (context.request.url.includes('/second')) {
context.locals.name = 'second'; context.locals.name = 'second';
} else if (context.request.url.includes('/redirect')) { } else if (context.request.url.includes('/redirect')) {
context.cookies.set('redirect', 'cookie redirect set');
return context.redirect('/', 302); return context.redirect('/', 302);
} }
return await next(); return await next();

View file

@ -1,5 +1,7 @@
--- ---
const data = Astro.locals; const data = Astro.locals;
const redirect = Astro.cookies.get("redirect");
console.log(redirect);
--- ---
<html> <html>
@ -10,5 +12,6 @@ const data = Astro.locals;
<span>Index</span> <span>Index</span>
<p>{data?.name}</p> <p>{data?.name}</p>
<p>{redirect.value}</p>
</body> </body>
</html> </html>

View file

@ -79,6 +79,13 @@ describe('Middleware in DEV mode', () => {
let html = await res.text(); let html = await res.text();
expect(html).to.contain('<h1>it works</h1>'); expect(html).to.contain('<h1>it works</h1>');
}); });
it('cookies should be preserved when calling a redirect', async () => {
let res = await fixture.fetch('/redirect');
let html = await res.text();
console.log(res.headers.get('set-cookie'));
expect(html).to.contain('<p>cookie redirect set</p>');
});
}); });
describe('Middleware in PROD mode, SSG', () => { describe('Middleware in PROD mode, SSG', () => {