-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use nitro to load polyfill at the earliest
- Loading branch information
1 parent
f5a6be1
commit f15b519
Showing
3 changed files
with
42 additions
and
38 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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { joinURL, cleanDoubleSlashes } from 'ufo' | ||
import { useRuntimeConfig } from "#imports"; | ||
|
||
/** | ||
* Due to an upstream bug in Nuxt 3 we need to stub the `defineNitroPlugin`. | ||
* See https://github.com/nuxt/nuxt/issues/18556 | ||
* | ||
* Once this issue is resolved, then this file can be updated to the following: | ||
* | ||
* ```ts | ||
* import { defineNitroPlugin } from '#imports' | ||
* ``` | ||
*/ | ||
import { NitroApp } from 'nitropack' | ||
|
||
export type NitroAppPlugin = (nitro: NitroApp) => void | ||
export function defineNitroPlugin (def: NitroAppPlugin): NitroAppPlugin { | ||
return def | ||
} | ||
/** | ||
* End of workaround | ||
*/ | ||
|
||
export default defineNitroPlugin((nitroApp) => { | ||
const config = useRuntimeConfig() | ||
|
||
const { src, isSelfHost } = config.public.nupolyon | ||
// Prepend the current runtime value of app.baseURL if configured for self-hosting | ||
const host = !isSelfHost ? src : cleanDoubleSlashes(joinURL(config.app.baseURL, src)) | ||
|
||
if (host) { | ||
// NOTE: By setting `nomodule` we make sure it executed directly upon loading, and also works in browsers which don't support ESM. | ||
const polyfill = `<script src="${host}" crossorigin="anonymous" nomodule></script>` | ||
nitroApp.hooks.hook('render:html', (html) => { | ||
html.head.unshift(polyfill) // insert at the beginning of the array | ||
}) | ||
} | ||
}) |