summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Emanuele Stoppa <my.burning@gmail.com> 2025-03-19 12:29:29 +0000
committerGravatar GitHub <noreply@github.com> 2025-03-19 12:29:29 +0000
commitd80ba2b27d33d2972ffa3242330fb00d0fc58ba9 (patch)
treeac4d7d2649e06995e5eb7a6c11cd66b91c89be90
parentb3d445f5148c2492434b4163d6be4b22e027294e (diff)
downloadastro-d80ba2b27d33d2972ffa3242330fb00d0fc58ba9.tar.gz
astro-d80ba2b27d33d2972ffa3242330fb00d0fc58ba9.tar.zst
astro-d80ba2b27d33d2972ffa3242330fb00d0fc58ba9.zip
fix(routing): don't add site to static redirects (#13447)
* fix(routing): don't add site to static redirects * chore: fix canonical tag * chore: fix canonical tag Co-authored-by: ascorbic <213306+ascorbic@users.noreply.github.com>
-rw-r--r--.changeset/dark-rooms-enter.md5
-rw-r--r--packages/astro/src/core/app/index.ts2
-rw-r--r--packages/astro/src/core/build/generate.ts7
-rw-r--r--packages/astro/src/core/routing/3xx.ts18
-rw-r--r--packages/astro/src/vite-plugin-astro-server/response.ts2
-rw-r--r--packages/astro/src/vite-plugin-astro-server/route.ts4
-rw-r--r--packages/astro/test/redirects.test.js21
7 files changed, 49 insertions, 10 deletions
diff --git a/.changeset/dark-rooms-enter.md b/.changeset/dark-rooms-enter.md
new file mode 100644
index 000000000..90b50593a
--- /dev/null
+++ b/.changeset/dark-rooms-enter.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Fixes an issue where `site` was added to the generated redirects.
diff --git a/packages/astro/src/core/app/index.ts b/packages/astro/src/core/app/index.ts
index f76408078..51d4e4c2f 100644
--- a/packages/astro/src/core/app/index.ts
+++ b/packages/astro/src/core/app/index.ts
@@ -290,7 +290,7 @@ export class App {
if (redirect !== url.pathname) {
const status = request.method === 'GET' ? 301 : 308;
- return new Response(redirectTemplate({ status, location: redirect, from: request.url }), {
+ return new Response(redirectTemplate({ status, relativeLocation: url.pathname, absoluteLocation: redirect, from: request.url }), {
status,
headers: {
location: redirect + url.search,
diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts
index cd3e6228c..0627f378f 100644
--- a/packages/astro/src/core/build/generate.ts
+++ b/packages/astro/src/core/build/generate.ts
@@ -548,7 +548,12 @@ async function generatePath(
const siteURL = config.site;
const location = siteURL ? new URL(locationSite, siteURL) : locationSite;
const fromPath = new URL(request.url).pathname;
- body = redirectTemplate({ status: response.status, location, from: fromPath });
+ body = redirectTemplate({
+ status: response.status,
+ absoluteLocation: location,
+ relativeLocation: locationSite,
+ from: fromPath,
+ });
if (config.compressHTML === true) {
body = body.replaceAll('\n', '');
}
diff --git a/packages/astro/src/core/routing/3xx.ts b/packages/astro/src/core/routing/3xx.ts
index c05d7a894..4b800b55f 100644
--- a/packages/astro/src/core/routing/3xx.ts
+++ b/packages/astro/src/core/routing/3xx.ts
@@ -1,19 +1,25 @@
export type RedirectTemplate = {
from?: string;
- location: string | URL;
+ absoluteLocation: string | URL;
status: number;
+ relativeLocation: string;
};
-export function redirectTemplate({ status, location, from }: RedirectTemplate) {
+export function redirectTemplate({
+ status,
+ absoluteLocation,
+ relativeLocation,
+ from,
+}: RedirectTemplate) {
// A short delay causes Google to interpret the redirect as temporary.
// https://developers.google.com/search/docs/crawling-indexing/301-redirects#metarefresh
const delay = status === 302 ? 2 : 0;
return `<!doctype html>
-<title>Redirecting to: ${location}</title>
-<meta http-equiv="refresh" content="${delay};url=${location}">
+<title>Redirecting to: ${relativeLocation}</title>
+<meta http-equiv="refresh" content="${delay};url=${relativeLocation}">
<meta name="robots" content="noindex">
-<link rel="canonical" href="${location}">
+<link rel="canonical" href="${absoluteLocation}">
<body>
- <a href="${location}">Redirecting ${from ? `from <code>${from}</code> ` : ''}to <code>${location}</code></a>
+ <a href="${relativeLocation}">Redirecting ${from ? `from <code>${from}</code> ` : ''}to <code>${relativeLocation}</code></a>
</body>`;
}
diff --git a/packages/astro/src/vite-plugin-astro-server/response.ts b/packages/astro/src/vite-plugin-astro-server/response.ts
index 03b25be1a..b265312e9 100644
--- a/packages/astro/src/vite-plugin-astro-server/response.ts
+++ b/packages/astro/src/vite-plugin-astro-server/response.ts
@@ -59,7 +59,7 @@ export function writeRedirectResponse(
statusCode: number,
location: string,
) {
- const html = redirectTemplate({ status: statusCode, location });
+ const html = redirectTemplate({ status: statusCode, absoluteLocation: location, relativeLocation: location });
res.writeHead(statusCode, {
Location: location,
'Content-Type': 'text/html',
diff --git a/packages/astro/src/vite-plugin-astro-server/route.ts b/packages/astro/src/vite-plugin-astro-server/route.ts
index 4129ba04c..cbaa768c4 100644
--- a/packages/astro/src/vite-plugin-astro-server/route.ts
+++ b/packages/astro/src/vite-plugin-astro-server/route.ts
@@ -302,10 +302,12 @@ export async function handleRoute({
) {
// If we're here, it means that the calling static redirect that was configured by the user
// We try to replicate the same behaviour that we provide during a static build
+ const location = response.headers.get('location')!;
response = new Response(
redirectTemplate({
status: response.status,
- location: response.headers.get('location')!,
+ absoluteLocation: location,
+ relativeLocation: location,
from: pathname,
}),
{
diff --git a/packages/astro/test/redirects.test.js b/packages/astro/test/redirects.test.js
index 451a9d75a..965e4be3c 100644
--- a/packages/astro/test/redirects.test.js
+++ b/packages/astro/test/redirects.test.js
@@ -317,4 +317,25 @@ describe('Astro.redirect', () => {
assert.equal(secretHtml.includes('to <code>/login</code>'), true);
});
});
+
+ describe('when site is specified', () => {
+ before(async () => {
+ process.env.STATIC_MODE = true;
+ fixture = await loadFixture({
+ root: './fixtures/redirects/',
+ output: 'static',
+ redirects: {
+ '/one': '/',
+ },
+ site: 'https://example.com',
+ });
+ await fixture.build();
+ });
+
+ it('Does not add it to the generated HTML file', async () => {
+ const secretHtml = await fixture.readFile('/secret/index.html');
+ assert.equal(secretHtml.includes('url=https://example.com/login'), false);
+ assert.equal(secretHtml.includes('url=/login'), true);
+ });
+ });
});