diff --git a/.changeset/eighty-cars-repeat.md b/.changeset/eighty-cars-repeat.md new file mode 100644 index 0000000..6f050cb --- /dev/null +++ b/.changeset/eighty-cars-repeat.md @@ -0,0 +1,5 @@ +--- +"astro-suspense": patch +--- + +Set vary header to avoid incorrect back button behavior. diff --git a/packages/astro-suspense/src/middleware.ts b/packages/astro-suspense/src/middleware.ts index 77b303f..ab8a07b 100644 --- a/packages/astro-suspense/src/middleware.ts +++ b/packages/astro-suspense/src/middleware.ts @@ -36,6 +36,9 @@ export const onRequest = defineMiddleware(async (ctx, next) => { ...response, headers: { ...response.headers, + vary: [response.headers.get("vary"), "astro-suspense-transition"] + .filter(Boolean) + .join("\n"), "content-type": "text/astro-suspense-transition-stream", }, }); @@ -43,7 +46,15 @@ export const onRequest = defineMiddleware(async (ctx, next) => { const stream = transformStream(ctx.locals.suspensePromises, response.body); - return new Response(stream, response); + return new Response(stream, { + ...response, + headers: { + ...response.headers, + vary: [response.headers.get("vary"), "astro-suspense-transition"] + .filter(Boolean) + .join("\n"), + }, + }); }); function transformStream(