mirror of
https://github.com/d0zingcat/BroadcastChannel.git
synced 2026-05-13 15:09:12 +00:00
feat: enhance middleware to set speculation rules header
This commit is contained in:
@@ -1,4 +1,10 @@
|
||||
export function onRequest(context, next) {
|
||||
export async function onRequest(context, next) {
|
||||
context.locals.SITE_URL = `${import.meta.env.SITE ?? ''}${import.meta.env.BASE_URL}`
|
||||
return next()
|
||||
|
||||
const response = await next()
|
||||
|
||||
if (!response.bodyUsed) {
|
||||
response.headers.set('Speculation-Rules', '"/rules/prefetch.json"')
|
||||
}
|
||||
return response
|
||||
};
|
||||
|
||||
22
src/pages/rules/prefetch.json.js
Normal file
22
src/pages/rules/prefetch.json.js
Normal file
@@ -0,0 +1,22 @@
|
||||
export const prerender = false
|
||||
|
||||
export async function GET() {
|
||||
return Response.json({
|
||||
prerender: [
|
||||
{
|
||||
urls: ['/', '/tags'],
|
||||
eagerness: 'eager',
|
||||
},
|
||||
],
|
||||
prefetch: [
|
||||
{
|
||||
where: { href_matches: ['/posts/*'] },
|
||||
eagerness: 'moderate',
|
||||
},
|
||||
],
|
||||
}, {
|
||||
headers: {
|
||||
'Content-Type': 'application/speculationrules+json',
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user