Redirects in Astro
You can configure redirects in your astro.config.ts file using the redirects option.
Redirects in Astro Config
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);
}
Redirects in Components
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>
Astro Middleware on Epycbyte
Executes before a request is processed on a site, allowing you to modify responses to user requests.
- Runs on all requests, but can be scoped to specific paths through a matcher config.
- Uses Epycbyte's lightweight Edge Runtime to keep costs low and responses fast.
Caching
Epycbyte automatically caches static files at the Edge after the first request, and stores them for up to 31 days on Epycbyte's Edge Network. Dynamic content can also be cached, and both dynamic and static caching behavior can be configured with Cache-Control headers.
Example: Serving Stale Content
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>
CDN Cache-Control Headers
You can also control how the cache behaves on any CDNs you may be using outside of Epycbyte's Edge Network with CDN Cache-Control Headers.
src/pages/ssr-with-swr-caching.astro
Astro.response.headers.set('Epycbyte-CDN-Cache-Control', 'max-age=3600');
Astro.response.headers.set('CDN-Cache-Control', 'max-age=60');
const time = new Date().toLocaleTimeString();
<h1>{time}</h1>
Caching on Epycbyte
Automatically optimizes and caches assets for the best performance.
- Requires no additional services to procure or set up.
- Supports zero-downtime rollouts.