Skip to content

Commit

Permalink
Merge pull request #271 from vejja/ensure-plugins-last
Browse files Browse the repository at this point in the history
fix(csp): ensure-plugins-last
  • Loading branch information
Baroshem authored Oct 30, 2023
2 parents a667406 + 076f819 commit e8b3651
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,48 @@ const registerSecurityNitroPlugins = (
)
}
})

// Make sure our nitro plugins will be applied last
// After all other third-party modules that might have loaded their own nitro plugins
nuxt.hook('nitro:init', nitro => {
const securityPluginsPrefix = normalize(
fileURLToPath(
new URL('./runtime/nitro/plugins', import.meta.url)
)
)
// SSR: Reorder plugins in Nitro options
nitro.options.plugins.sort((a, b) => {
if (a.startsWith(securityPluginsPrefix)) {
if (b.startsWith(securityPluginsPrefix)) {
return 0
} else {
return 1
}
} else {
if (b.startsWith(securityPluginsPrefix)) {
return -1
} else {
return 0
}
}
})
// SSG: Reorder plugins in Nitro hook
nitro.hooks.hook('prerender:config', config => {
config.plugins?.sort((a, b) => {
if (a?.startsWith(securityPluginsPrefix)) {
if (b?.startsWith(securityPluginsPrefix)) {
return 0
} else {
return 1
}
} else {
if (b?.startsWith(securityPluginsPrefix)) {
return -1
} else {
return 0
}
}
})
})
})
}

0 comments on commit e8b3651

Please sign in to comment.