This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(nuxt): improve link prefetching (#8225)
- Loading branch information
Showing
7 changed files
with
73 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/nuxt/src/app/plugins/cross-origin-prefetch.client.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { ref } from 'vue' | ||
import { parseURL } from 'ufo' | ||
import { defineNuxtPlugin, useHead } from '#app' | ||
|
||
export default defineNuxtPlugin((nuxtApp) => { | ||
const externalURLs = ref(new Set<string>()) | ||
useHead({ | ||
script: [ | ||
() => ({ | ||
type: 'speculationrules', | ||
innerHTML: JSON.stringify({ | ||
prefetch: [ | ||
{ | ||
source: 'list', | ||
urls: [...externalURLs.value], | ||
requires: ['anonymous-client-ip-when-cross-origin'] | ||
} | ||
] | ||
}) | ||
}) | ||
] | ||
}) | ||
nuxtApp.hook('link:prefetch', (url) => { | ||
const { protocol } = parseURL(url) | ||
if (protocol && ['http:', 'https:'].includes(protocol)) { | ||
externalURLs.value.add(url) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters