-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: utilize timestamps in build and renderDocument for AUS (#6964)
* feat: utilize timestamps in build and renderDocument for AUS * refactor: Apply suggestions from code review Co-authored-by: Espen Hovlandsdal <[email protected]> * fix: set to no-cache for help with consistency --------- Co-authored-by: Espen Hovlandsdal <[email protected]>
- Loading branch information
1 parent
f2ddfa7
commit a1da8cc
Showing
7 changed files
with
125 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* This script takes the import map from the `#__imports` script tag, | ||
* modifies relevant URLs that match the sanity-cdn hostname by replacing | ||
* the existing timestamp in the sanity-cdn URLs with a new runtime timestamp, | ||
* and injects the modified import map back into the HTML. | ||
* | ||
* This will be injected into the HTML of the user's bundle. | ||
* | ||
* Note that this is in a separate constants file to prevent "Cannot access | ||
* before initialization" errors. | ||
*/ | ||
export const TIMESTAMPED_IMPORTMAP_INJECTOR_SCRIPT = `<script> | ||
// auto-generated script to add import map with timestamp | ||
const importsJson = document.getElementById('__imports')?.textContent; | ||
const { imports = {}, ...rest } = importsJson ? JSON.parse(importsJson) : {}; | ||
const importMapEl = document.createElement('script'); | ||
importMapEl.type = 'importmap'; | ||
const newTimestamp = \`/t\${Math.floor(Date.now() / 1000)}\`; | ||
importMapEl.textContent = JSON.stringify({ | ||
imports: Object.fromEntries( | ||
Object.entries(imports).map(([specifier, path]) => { | ||
try { | ||
const url = new URL(path); | ||
if (/^sanity-cdn\\.[a-zA-Z]+$/.test(url.hostname)) { | ||
url.pathname = url.pathname.replace(/\\/t\\d+/, newTimestamp); | ||
} | ||
return [specifier, url.toString()]; | ||
} catch { | ||
return [specifier, path]; | ||
} | ||
}) | ||
), | ||
...rest, | ||
}); | ||
document.head.appendChild(importMapEl); | ||
</script>` |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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