-
Notifications
You must be signed in to change notification settings - Fork 9
feat: prerender hint based on ssrContext access #184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
cd8cd49
e0ff4e1
48c8454
aa182b4
279939e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <template> | ||
| <div class="test"> | ||
| Hello. Could this page be prerendered ? {{ shouldPrerender }} | ||
| </div> | ||
| </template> | ||
|
|
||
| <script setup lang="ts"> | ||
| import { useNuxtApp, useState } from '#app' | ||
| import { onMounted } from 'vue' | ||
|
|
||
| const shouldPrerender = useState('shouldPrerender', () => true) | ||
|
|
||
| if (import.meta.server) { | ||
| // ssr context access will be detected by the prerender plugin | ||
| console.log(useNuxtApp().ssrContext?.event.context.shouldPrerender) | ||
| } | ||
|
|
||
| onMounted(() => { | ||
| // @ts-expect-error for demo purposes | ||
| if (typeof window.__NUXT_HINTS_SHOULD_PRERENDER__ !== 'undefined') { | ||
| // @ts-expect-error for demo purposes | ||
| shouldPrerender.value = window.__NUXT_HINTS_SHOULD_PRERENDER__ | ||
| } | ||
| }) | ||
| </script> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import type { NitroAppPlugin } from 'nitropack' | ||
|
|
||
| export default <NitroAppPlugin> function nitroHintsPlugin(nitroApp) { | ||
| nitroApp.hooks.hook('render:before', ({ event }) => { | ||
| event.context.shouldPrerender = true | ||
| }) | ||
|
|
||
| nitroApp.hooks.hook('render:html', (htmlContext, { event }) => { | ||
| if (event.context.shouldPrerender === false) { | ||
| htmlContext.bodyAppend.push( | ||
| `<script>window.__NUXT_HINTS_SHOULD_PRERENDER__ = false</script>`, | ||
| ) | ||
| } | ||
| else { | ||
| htmlContext.bodyAppend.push( | ||
| `<script>window.__NUXT_HINTS_SHOULD_PRERENDER__ = true</script>`, | ||
| ) | ||
| } | ||
| }) | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,44 @@ | ||||||||||||||||||||||||||||||||||||
| import { defineNuxtPlugin } from '#imports' | ||||||||||||||||||||||||||||||||||||
| import { getStackTraceLines } from './utils' | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| export default defineNuxtPlugin({ | ||||||||||||||||||||||||||||||||||||
| name: 'hints:prerender-detection', | ||||||||||||||||||||||||||||||||||||
| setup(nuxtApp) { | ||||||||||||||||||||||||||||||||||||
| const event = nuxtApp.ssrContext!.event | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| let watching = true | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| const ssrContext = nuxtApp.ssrContext | ||||||||||||||||||||||||||||||||||||
| // Access to any property on ssrContext will mark the page as non-prerenderable | ||||||||||||||||||||||||||||||||||||
| Object.defineProperty(nuxtApp, 'ssrContext', { | ||||||||||||||||||||||||||||||||||||
| get() { | ||||||||||||||||||||||||||||||||||||
| if (watching && isUserLandCode()) { | ||||||||||||||||||||||||||||||||||||
| // Mark as non-prerenderable | ||||||||||||||||||||||||||||||||||||
| // we only want to do this when user-land code is being executed | ||||||||||||||||||||||||||||||||||||
| // to avoid false positives from internal framework code | ||||||||||||||||||||||||||||||||||||
| // it's better to be slightly overzealous here than miss actual user code | ||||||||||||||||||||||||||||||||||||
| event.context.shouldPrerender = false | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| return ssrContext | ||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| nuxtApp.hook('app:rendered', () => { | ||||||||||||||||||||||||||||||||||||
| watching = false | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||
| order: -100000, | ||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||
| * Determine if the current execution context is user-land code | ||||||||||||||||||||||||||||||||||||
| * by analyzing the stack trace. | ||||||||||||||||||||||||||||||||||||
| * Should ignore the first line as the fn call is this function itself. | ||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||
| function isUserLandCode(offset: number = 1): boolean { | ||||||||||||||||||||||||||||||||||||
| const stack = getStackTraceLines() | ||||||||||||||||||||||||||||||||||||
| const lines = stack.slice(2) | ||||||||||||||||||||||||||||||||||||
| const line = lines[offset] | ||||||||||||||||||||||||||||||||||||
| const isUserLand = !line?.includes('node_modules') && !line?.includes('node:internal') | ||||||||||||||||||||||||||||||||||||
| return isUserLand | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+38
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: If π Proposed fix function isUserLandCode(offset: number = 1): boolean {
const stack = getStackTraceLines()
const lines = stack.slice(2)
const line = lines[offset]
+ if (!line) {
+ return false
+ }
- const isUserLand = !line?.includes('node_modules') && !line?.includes('node:internal')
+ const isUserLand = !line.includes('node_modules') && !line.includes('node:internal')
return isUserLand
}π Committable suggestion
Suggested change
π€ Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,13 @@ | ||||||||||||||||||
| /** | ||||||||||||||||||
| * Get all stacktrace lines without the current file | ||||||||||||||||||
| * Don't use in build files if we ever goes into build mode nuxt hiints. Thank you. | ||||||||||||||||||
| */ | ||||||||||||||||||
|
Comment on lines
+1
to
+4
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo in comment. "hiints" should be "hints". /**
* Get all stacktrace lines without the current file
- * Don't use in build files if we ever goes into build mode nuxt hiints. Thank you.
+ * Don't use in build files if we ever go into build mode nuxt hints. Thank you.
*/π Committable suggestion
Suggested change
π€ Prompt for AI Agents |
||||||||||||||||||
| export function getStackTraceLines(): string[] { | ||||||||||||||||||
| const stackObject: { stack: string } = {} as { stack: string } | ||||||||||||||||||
| Error.captureStackTrace(stackObject) | ||||||||||||||||||
|
|
||||||||||||||||||
| return stackObject.stack | ||||||||||||||||||
| .split('\n') | ||||||||||||||||||
| .slice(1) | ||||||||||||||||||
| .map(line => line.trim()) | ||||||||||||||||||
| } | ||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it depends what you're accessing. ssrContext.event.url would be fine, for example.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any other property you think it is fine ? or should we check request headers and event.context acess only ?