Ich möchte Spekulationsregeln implementieren https://developer.mozilla.org/en-us/doc ... _rules_api in mein NextJS-Projekt und haben dies mit dem folgenden Code erfolgreich erledigt.{JSON.stringify({
prefetch: [
{
source: "list",
urls: "www.example.com",
eagerness: "moderate",
},
],
})}
< /code>
Dies wird korrekt ausgeführt und funktioniert wie erwartet. Ich muss jedoch die Spekulationsregeln aktualisieren, wenn eine bestimmte Aktion erfolgt, um sicherzustellen, dass die Daten nicht veraltet sind. Dazu bin ich den Chrome-Dokumenten verfolgt.async function refreshSpeculations() {
const speculationScripts = document.querySelectorAll('script[type="speculationrules"]');
for (const speculationScript of speculationScripts) {
// Get the current rules as JSON text
const ruleSet = speculationScript.textContent;
// Remove the existing script to reset prerendering
speculationScript.remove();
// Await for a microtask before re-inserting.
await Promise.resolve();
// Reinsert rule in a new speculation rules script
const newScript = document.createElement('script');
newScript.type = 'speculationrules';
newScript.textContent = ruleSet;
console.log(newScript);
// Append the new script back to the document
document.body.appendChild(newScript);
}
}
< /code>
Dies wird korrekt ausgeführt, aber anstatt das Skriptelement aus dem Kopf -Tag insgesamt zu entfernen, scheint es es zu entfernen und es dann am unteren Rand des Head -Tags anzuhängen und es an den Körper anzuhängen. document?>
NextJS - Skript -Tag, das nicht entfernt wird ⇐ JavaScript
-
- Similar Topics
- Replies
- Views
- Last post