Skip to content

Commit 1643090

Browse files
committed
Revert "feat!: default compoments for 3xx"
This reverts commit 22320f1.
1 parent 22320f1 commit 1643090

File tree

7 files changed

+46
-110
lines changed

7 files changed

+46
-110
lines changed

packages/astro/src/core/app/index.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ import {
2222
} from '../path.js';
2323
import { RenderContext } from '../render-context.js';
2424
import { createAssetLink } from '../render/ssr-element.js';
25+
import { redirectTemplate } from '../routing/3xx.js';
2526
import { ensure404Route } from '../routing/astro-designed-error-pages.js';
2627
import { createDefaultRoutes } from '../routing/default.js';
2728
import { matchRoute } from '../routing/match.js';
2829
import { type AstroSession, PERSIST_SYMBOL } from '../session.js';
2930
import { AppPipeline } from './pipeline.js';
30-
import { default3xxPage } from '../routing/3xx.js';
3131

3232
export { deserializeManifest } from './common.js';
3333

@@ -313,12 +313,20 @@ export class App {
313313

314314
if (redirect !== url.pathname) {
315315
const status = request.method === 'GET' ? 301 : 308;
316-
return default3xxPage({
317-
status,
318-
relativeLocation: url.pathname,
319-
absoluteLocation: redirect,
320-
from: request.url,
321-
});
316+
return new Response(
317+
redirectTemplate({
318+
status,
319+
relativeLocation: url.pathname,
320+
absoluteLocation: redirect,
321+
from: request.url,
322+
}),
323+
{
324+
status,
325+
headers: {
326+
location: redirect + url.search,
327+
},
328+
},
329+
);
322330
}
323331

324332
addCookieHeader = renderOptions?.addCookieHeader;

packages/astro/src/core/build/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import { getRedirectLocationOrThrow, routeIsRedirect } from '../redirects/index.
3636
import { RenderContext } from '../render-context.js';
3737
import { callGetStaticPaths } from '../render/route-cache.js';
3838
import { createRequest } from '../request.js';
39-
import redirectTemplate from '../../template/3xx.js';
39+
import { redirectTemplate } from '../routing/3xx.js';
4040
import { matchRoute } from '../routing/match.js';
4141
import { stringifyParams } from '../routing/params.js';
4242
import { getOutputFilename } from '../util.js';

packages/astro/src/core/constants.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ export const ROUTE_TYPE_HEADER = 'X-Astro-Route-Type';
4646
*/
4747
export const DEFAULT_404_COMPONENT = 'astro-default-404.astro';
4848

49-
/**
50-
* The value of the `component` field of the default 3xx page, which is used when there is no user-provided 3xx.astro page.
51-
*/
52-
export const DEFAULT_3XX_COMPONENT = 'astro-default-3xx.astro';
53-
5449
/**
5550
* A response with one of these status codes will create a redirect response.
5651
*/
Lines changed: 17 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,25 @@
1-
import redirectTemplate, { type RedirectTemplateOptions } from '../../template/3xx.js';
2-
import type { RoutesList, ComponentInstance } from '../../types/astro.js';
3-
import type { RouteData } from '../../types/public/index.js';
4-
import { DEFAULT_3XX_COMPONENT } from '../constants.js';
5-
6-
export const DEFAULT_3XX_ROUTE: RouteData = {
7-
component: DEFAULT_3XX_COMPONENT,
8-
generate: () => '',
9-
params: [],
10-
pattern: /\/3xx/,
11-
prerender: false,
12-
pathname: '/3xx',
13-
segments: [[{ content: '3xx', dynamic: false, spread: false }]],
14-
type: 'page',
15-
route: '/3xx',
16-
fallbackRoutes: [],
17-
isIndex: false,
18-
origin: 'internal',
1+
type RedirectTemplate = {
2+
from?: string;
3+
absoluteLocation: string | URL;
4+
status: number;
5+
relativeLocation: string;
196
};
207

21-
export function ensure3xxRoute(manifest: RoutesList) {
22-
if (!manifest.routes.some((route) => route.route === '/3xx')) {
23-
manifest.routes.push(DEFAULT_3XX_ROUTE);
24-
}
25-
return manifest;
26-
}
27-
28-
export async function default3xxPage({
8+
export function redirectTemplate({
299
status,
3010
absoluteLocation,
3111
relativeLocation,
3212
from,
33-
}: RedirectTemplateOptions) {
34-
return new Response(
35-
redirectTemplate({
36-
status,
37-
absoluteLocation,
38-
relativeLocation,
39-
from,
40-
}),
41-
{
42-
status,
43-
headers: { 'Content-Type': 'text/html' },
44-
},
45-
);
46-
}
47-
48-
// mark the function as an AstroComponentFactory for the rendering internals
49-
default3xxPage.isAstroComponentFactory = true;
50-
51-
export const default3xxInstance: ComponentInstance = {
52-
default: default3xxPage,
53-
};
54-
55-
// A short delay causes Google to interpret the redirect as temporary.
56-
// https://developers.google.com/search/docs/crawling-indexing/301-redirects#metarefresh
57-
58-
export async function injectRedirectMetaTags(html: string, location: string, status: number) {
13+
}: RedirectTemplate) {
14+
// A short delay causes Google to interpret the redirect as temporary.
15+
// https://developers.google.com/search/docs/crawling-indexing/301-redirects#metarefresh
5916
const delay = status === 302 ? 2 : 0;
60-
return html.replace(
61-
/<head[^>]*>/i,
62-
`$&
63-
<meta http-equiv="refresh" content="${delay};url=${location}">
64-
<meta name="robots" content="noindex">
65-
<link rel="canonical" href="${location}">`,
66-
);
17+
return `<!doctype html>
18+
<title>Redirecting to: ${relativeLocation}</title>
19+
<meta http-equiv="refresh" content="${delay};url=${relativeLocation}">
20+
<meta name="robots" content="noindex">
21+
<link rel="canonical" href="${absoluteLocation}">
22+
<body>
23+
<a href="${relativeLocation}">Redirecting ${from ? `from <code>${from}</code> ` : ''}to <code>${relativeLocation}</code></a>
24+
</body>`;
6725
}

packages/astro/src/core/routing/default.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import type { ComponentInstance } from '../../types/astro.js';
22
import type { SSRManifest } from '../app/types.js';
3-
import { DEFAULT_3XX_COMPONENT, DEFAULT_404_COMPONENT } from '../constants.js';
3+
import { DEFAULT_404_COMPONENT } from '../constants.js';
44
import {
55
SERVER_ISLAND_COMPONENT,
66
SERVER_ISLAND_ROUTE,
77
createEndpoint as createServerIslandEndpoint,
88
} from '../server-islands/endpoint.js';
9-
import { default3xxInstance, DEFAULT_3XX_ROUTE } from './3xx.js';
109
import { DEFAULT_404_ROUTE, default404Instance } from './astro-designed-error-pages.js';
1110

1211
type DefaultRouteParams = {
@@ -16,11 +15,7 @@ type DefaultRouteParams = {
1615
component: string;
1716
};
1817

19-
export const DEFAULT_COMPONENTS = [
20-
DEFAULT_3XX_COMPONENT,
21-
DEFAULT_404_COMPONENT,
22-
SERVER_ISLAND_COMPONENT,
23-
];
18+
export const DEFAULT_COMPONENTS = [DEFAULT_404_COMPONENT, SERVER_ISLAND_COMPONENT];
2419

2520
export function createDefaultRoutes(manifest: SSRManifest): DefaultRouteParams[] {
2621
const root = new URL(manifest.hrefRoot);
@@ -31,12 +26,6 @@ export function createDefaultRoutes(manifest: SSRManifest): DefaultRouteParams[]
3126
route: DEFAULT_404_ROUTE.route,
3227
component: DEFAULT_404_COMPONENT,
3328
},
34-
{
35-
instance: default3xxInstance,
36-
matchesComponent: (filePath) => filePath.href === new URL(DEFAULT_3XX_COMPONENT, root).href,
37-
route: DEFAULT_3XX_ROUTE.route,
38-
component: DEFAULT_3XX_COMPONENT,
39-
},
4029
{
4130
instance: createServerIslandEndpoint(manifest),
4231
matchesComponent: (filePath) => filePath.href === new URL(SERVER_ISLAND_COMPONENT, root).href,

packages/astro/src/template/3xx.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

packages/astro/src/vite-plugin-astro-server/route.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { routeIsRedirect } from '../core/redirects/index.js';
1414
import { RenderContext } from '../core/render-context.js';
1515
import { getProps } from '../core/render/index.js';
1616
import { createRequest } from '../core/request.js';
17+
import { redirectTemplate } from '../core/routing/3xx.js';
1718
import { matchAllRoutes } from '../core/routing/index.js';
1819
import { isRoute3xx, isRoute404, isRoute500 } from '../core/routing/match.js';
1920
import { PERSIST_SYMBOL } from '../core/session.js';
@@ -22,7 +23,6 @@ import type { ComponentInstance, RoutesList } from '../types/astro.js';
2223
import type { RouteData } from '../types/public/internal.js';
2324
import type { DevPipeline } from './pipeline.js';
2425
import { writeSSRResult, writeWebResponse } from './response.js';
25-
import { injectRedirectMetaTags } from '../core/routing/3xx.js';
2626

2727
type AsyncReturnType<T extends (...args: any) => Promise<any>> = T extends (
2828
...args: any
@@ -339,7 +339,16 @@ export async function handleRoute({
339339
const headers = Object.fromEntries(redirectResponse.headers.entries());
340340

341341
const html = await redirectResponse.text();
342-
const injectedHtml = await injectRedirectMetaTags(html, location, response.status);
342+
343+
const delay = response.status === 302 ? 2 : 0;
344+
345+
const injectedHtml = html.replace(
346+
/<head[^>]*>/i,
347+
`$&
348+
<meta http-equiv="refresh" content="${delay};url=${location}">
349+
<meta name="robots" content="noindex">
350+
<link rel="canonical" href="${location}">`,
351+
);
343352

344353
response = new Response(injectedHtml, {
345354
status: response.status,

0 commit comments

Comments
 (0)