RedirectPage Component
A static, client-side redirect stub for hosts without server-side redirect
support (GitHub Pages, S3, any plain CDN). Renders a page that redirects via
<meta http-equiv="refresh">, a <link rel="canonical"> for search
engines, and a window.location.replace() fallback for browsers that
ignore meta-refresh.
Basic Usage
---import { RedirectPage } from '@lad-sapienza/scms-core';export const prerender = true;---<RedirectPage target="/it/chi-siamo" />Props API
| Prop | Type | Default | Description |
|---|---|---|---|
target | string | — | Destination URL to redirect to (required) |
title | string | "Redirecting…" | Page <title>, shown briefly before the redirect fires |
When to use this vs. Astro’s native redirects
Astro’s own redirects
config option in astro.config.mjs generates the same kind of stub
automatically for output: 'static' sites, and should be preferred
whenever it applies — it’s zero extra code:
export default defineConfig({ redirects: { '/chi-siamo': '/it/chi-siamo', },});This works fine even when the destination is produced by a dynamic route
(e.g. /it/chi-siamo generated by src/pages/[locale]/chi-siamo.astro),
as long as the source path itself is static. Astro only rejects a
redirect when the source has dynamic segments of its own and the
destination needs a parameter the source doesn’t carry — the common case
being a locale prefix added to every URL of a content collection:
redirects: { // Fails at build time: InvalidRedirectDestination — the destination // doesn't match any registered route, since `it` can't be hardcoded // into a path that's supposed to satisfy the [locale] param. '/notizie/[...slug]': '/it/notizie/[...slug]',}For that case — redirecting every entry of a collection whose new URL
carries a param the old flat URL didn’t have — use RedirectPage with
getStaticPaths, reading the slug list straight from the collection so it
can’t drift out of sync with the content:
---import { RedirectPage } from '@lad-sapienza/scms-core';import { getCollection } from 'astro:content';
export const prerender = true;
export async function getStaticPaths() { const articles = await getCollection('notizie'); return articles .filter((a) => a.id.startsWith('it/')) .map((a) => ({ params: { slug: a.id.slice('it/'.length) } }));}
const { slug } = Astro.params;---<RedirectPage target={`/it/notizie/${slug}/`} />See the RedirectPage README
in the @lad-sapienza/scms-core repo for the full recipe and rationale.
Related Components
- TableOfContents: another plain
.astroutility component, same import style - Works together with Astro’s
redirectsconfig rather than replacing it — see above
See the documentation index for the rest of the component reference.