Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions modules/isr-fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export default defineNuxtModule({
}

nuxt.hook('nitro:init', nitro => {
const htmlFallback = 'spa.prerender-fallback.html'
const jsonFallback = 'payload-fallback.json'
nitro.hooks.hook('compiled', () => {
const spaTemplate = readFileSync(nitro.options.output.publicDir + '/200.html', 'utf-8')
for (const path of [
Expand All @@ -26,14 +28,10 @@ export default defineNuxtModule({
'package/[org]/[name]/v/[version]',
'',
]) {
const outputPath = resolve(
nitro.options.output.serverDir,
'..',
path,
'spa.prerender-fallback.html',
)
const outputPath = resolve(nitro.options.output.serverDir, '..', path, htmlFallback)
mkdirSync(resolve(nitro.options.output.serverDir, '..', path), { recursive: true })
writeFileSync(outputPath, spaTemplate)
writeFileSync(outputPath.replace(htmlFallback, jsonFallback), '{}')
}
})
})
Expand Down
22 changes: 15 additions & 7 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,14 @@ export default defineNuxtConfig({
},
},
// pages
'/package/:name': { isr: getISRConfig(60, true) },
'/package/:name/v/:version': { isr: getISRConfig(60, true) },
'/package/:org/:name': { isr: getISRConfig(60, true) },
'/package/:org/:name/v/:version': { isr: getISRConfig(60, true) },
'/package/:name': { isr: getISRConfig(60, { fallback: 'html' }) },
'/package/:name/_payload.json': { isr: getISRConfig(60, { fallback: 'json' }) },
'/package/:name/v/:version': { isr: getISRConfig(60, { fallback: 'html' }) },
'/package/:name/v/:version/_payload.json': { isr: getISRConfig(60, { fallback: 'json' }) },
'/package/:org/:name': { isr: getISRConfig(60, { fallback: 'html' }) },
'/package/:org/:name/_payload.json': { isr: getISRConfig(60, { fallback: 'json' }) },
'/package/:org/:name/v/:version': { isr: getISRConfig(60, { fallback: 'html' }) },
'/package/:org/:name/v/:version/_payload.json': { isr: getISRConfig(60, { fallback: 'json' }) },
// infinite cache (versioned - doesn't change)
'/package-code/**': { isr: true, cache: { maxAge: 365 * 24 * 60 * 60 } },
'/package-docs/**': { isr: true, cache: { maxAge: 365 * 24 * 60 * 60 } },
Expand Down Expand Up @@ -281,11 +285,15 @@ export default defineNuxtConfig({
},
})

function getISRConfig(expirationSeconds: number, fallback = false) {
if (fallback) {
interface ISRConfigOptions {
fallback?: 'html' | 'json'
}
function getISRConfig(expirationSeconds: number, options: ISRConfigOptions = {}) {
if (options.fallback) {
return {
expiration: expirationSeconds,
fallback: 'spa.prerender-fallback.html',
fallback:
options.fallback === 'html' ? 'spa.prerender-fallback.html' : 'payload-fallback.json',
} as { expiration: number }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

# First, find the nuxt.config.ts file
find . -name "nuxt.config.ts" -type f

Repository: npmx-dev/npmx.dev

Length of output: 99


🏁 Script executed:

# Check the file size first
wc -l ./nuxt.config.ts

# Then read lines 280-305 to get full context
sed -n '280,305p' ./nuxt.config.ts

Repository: npmx-dev/npmx.dev

Length of output: 604


🏁 Script executed:

# Check if getISRConfig is called elsewhere
rg "getISRConfig" ./nuxt.config.ts

# Also check for any imports or exports
rg "getISRConfig" . --type ts --type tsx

Repository: npmx-dev/npmx.dev

Length of output: 927


🏁 Script executed:

# Check the TypeScript strict settings to confirm strict type-safety is enforced
cat ./tsconfig.json | grep -A 20 '"strict"'

Repository: npmx-dev/npmx.dev

Length of output: 43


🏁 Script executed:

# Check if tsconfig.json exists
ls -la tsconfig.json 2>&1 || echo "File not found"

# Try reading the file
cat tsconfig.json 2>&1 | head -50

Repository: npmx-dev/npmx.dev

Length of output: 442


Remove the type assertion to restore type safety.

The as { expiration: number } cast on line 296 hides the fallback field from the return type, even though the function includes it in the returned object when options.fallback is provided. This violates strict type-safety requirements. Define an explicit return type that includes an optional fallback property and remove the assertion.

🔧 Proposed fix
 interface ISRConfigOptions {
   fallback?: 'html' | 'json'
 }
+interface ISRConfig {
+  expiration: number
+  fallback?: 'spa.prerender-fallback.html' | 'payload-fallback.json'
+}
-function getISRConfig(expirationSeconds: number, options: ISRConfigOptions = {}) {
+function getISRConfig(expirationSeconds: number, options: ISRConfigOptions = {}): ISRConfig {
   if (options.fallback) {
     return {
       expiration: expirationSeconds,
       fallback:
         options.fallback === 'html' ? 'spa.prerender-fallback.html' : 'payload-fallback.json',
-    } as { expiration: number }
+    }
   }
   return {
     expiration: expirationSeconds,
   }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
interface ISRConfigOptions {
fallback?: 'html' | 'json'
}
function getISRConfig(expirationSeconds: number, options: ISRConfigOptions = {}) {
if (options.fallback) {
return {
expiration: expirationSeconds,
fallback: 'spa.prerender-fallback.html',
fallback:
options.fallback === 'html' ? 'spa.prerender-fallback.html' : 'payload-fallback.json',
} as { expiration: number }
interface ISRConfigOptions {
fallback?: 'html' | 'json'
}
interface ISRConfig {
expiration: number
fallback?: 'spa.prerender-fallback.html' | 'payload-fallback.json'
}
function getISRConfig(expirationSeconds: number, options: ISRConfigOptions = {}): ISRConfig {
if (options.fallback) {
return {
expiration: expirationSeconds,
fallback:
options.fallback === 'html' ? 'spa.prerender-fallback.html' : 'payload-fallback.json',
}
}
return {
expiration: expirationSeconds,
}
}

}
return {
Expand Down
Loading