Redirects in Astro
You can configure redirects in your astro.config.ts file using the redirects option.
import { defineConfig } from 'astro/config';
export default defineConfig({ redirects: { '/old-page': '/new-page', }, });
### Redirects in Server Endpoints
You can also return a redirect from a Server Endpoint using the `redirect` utility:
src/pages/links/[id].ts
export async function GET({ params, redirect }: APIRoute) {
return redirect('/redirect-path', 307);
}
You can redirect from within Astro components with Astro.redirect():
src/pages/account.astro
import { isLoggedIn } from '../utils';
const cookie = Astro.request.headers.get('cookie');
if (!isLoggedIn(cookie)) {
return Astro.redirect('/login');
}
<h1>You can only see this page while logged in</h1>
Executes before a request is processed on a site, allowing you to modify responses to user requests.
Esadax automatically caches static files at the Edge after the first request, and stores them for up to 31 days on Esadax's Edge Network. Dynamic content can also be cached, and both dynamic and static caching behavior can be configured with Cache-Control headers.
The following Astro component will show a new time every 10 seconds. It does by setting a 10 second max age on the contents of the page, then serving stale content while new content is being rendered on the server when that age is exceeded.
src/pages/ssr-with-swr-caching.astro
Astro.response.headers.set('Cache-Control', 's-maxage=10, stale-while-revalidate');
const time = new Date().toLocaleTimeString();
<h1>{time}</h1>
You can also control how the cache behaves on any CDNs you may be using outside of Esadax's Edge Network with CDN Cache-Control Headers.
src/pages/ssr-with-swr-caching.astro
Astro.response.headers.set('Esadax-CDN-Cache-Control', 'max-age=3600');
Astro.response.headers.set('CDN-Cache-Control', 'max-age=60');
const time = new Date().toLocaleTimeString();
<h1>{time}</h1>
Automatically optimizes and caches assets for the best performance.