Skip to content

Commit

Permalink
fix: safer page SSR check
Browse files Browse the repository at this point in the history
Fixes #32
  • Loading branch information
harlan-zw committed Nov 13, 2023
1 parent 7f3320a commit c59a4cc
Showing 1 changed file with 1 addition and 13 deletions.
14 changes: 1 addition & 13 deletions src/runtime/nitro-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,14 @@ export default defineNitroPlugin((nitro) => {
return

let extraScripts = ''
let isPageSSR = true
if (currentMode === 'init') {
const ASSET_RE = new RegExp(`<script[^>]*src="${config.app.buildAssetsDir}[^>]+><\\/script>`)

const LINK_ASSET_RE = new RegExp(`<link rel="modulepreload" as="script" [^>]*href="${config.app.buildAssetsDir}[^>]+>`, 'g')
htmlContext.head = htmlContext.head.map((head: string) => head.replaceAll(LINK_ASSET_RE, ''))

const toLoad: Record<string, any>[] = []
const ssrContext = htmlContext.bodyAppend.find(b => b.includes('window.__NUXT__'))
const NUXT_DATA_RE = /<script type="application\/json" id="__NUXT_DATA__"[^>]*>(.*?)<\/script[^>]*>/g
const regexResult = NUXT_DATA_RE.exec(ssrContext)
const nuxtData = regexResult && regexResult[1] ? JSON.parse(regexResult[1]) : null
if (nuxtData && nuxtData.length >= 2) {
const serverRenderedIndex = nuxtData[1].serverRendered
isPageSSR = nuxtData[serverRenderedIndex]
}
else {
isPageSSR = ssrContext.includes('serverRendered:true')
}

const isPageSSR = htmlContext.bodyAppend.some((b: string) => b.includes('$snuxt-delay-hydration-mode'))
if (!isPageSSR)
return

Expand Down

0 comments on commit c59a4cc

Please sign in to comment.