This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nuxt): payload rendering support (#6455)
Co-authored-by: Daniel Roe <[email protected]>
- Loading branch information
Showing
14 changed files
with
275 additions
and
10 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 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,60 @@ | ||
import { parseURL, joinURL } from 'ufo' | ||
import { useNuxtApp } from '../nuxt' | ||
import { useHead } from '#app' | ||
|
||
interface LoadPayloadOptions { | ||
fresh?: boolean | ||
hash?: string | ||
} | ||
|
||
export function loadPayload (url: string, opts: LoadPayloadOptions = {}) { | ||
if (process.server) { return null } | ||
const payloadURL = _getPayloadURL(url, opts) | ||
const nuxtApp = useNuxtApp() | ||
const cache = nuxtApp._payloadCache = nuxtApp._payloadCache || {} | ||
if (cache[payloadURL]) { | ||
return cache[payloadURL] | ||
} | ||
cache[url] = _importPayload(payloadURL).then((payload) => { | ||
if (!payload) { | ||
delete cache[url] | ||
return null | ||
} | ||
return payload | ||
}) | ||
return cache[url] | ||
} | ||
|
||
export function preloadPayload (url: string, opts: LoadPayloadOptions = {}) { | ||
const payloadURL = _getPayloadURL(url, opts) | ||
useHead({ | ||
link: [ | ||
{ rel: 'modulepreload', href: payloadURL } | ||
] | ||
}) | ||
} | ||
|
||
// --- Internal --- | ||
|
||
function _getPayloadURL (url: string, opts: LoadPayloadOptions = {}) { | ||
const parsed = parseURL(url) | ||
if (parsed.search) { | ||
throw new Error('Payload URL cannot contain search params: ' + url) | ||
} | ||
const hash = opts.hash || (opts.fresh ? Date.now() : '') | ||
return joinURL(parsed.pathname, hash ? `_payload.${hash}.js` : '_payload.js') | ||
} | ||
|
||
async function _importPayload (payloadURL: string) { | ||
if (process.server) { return null } | ||
const res = await import(/* webpackIgnore: true */ /* @vite-ignore */ payloadURL).catch((err) => { | ||
console.warn('[nuxt] Cannot load payload ', payloadURL, err) | ||
}) | ||
return res?.default || null | ||
} | ||
|
||
export function isPrerendered () { | ||
// Note: Alternative for server is checking x-nitro-prerender header | ||
const nuxtApp = useNuxtApp() | ||
return !!nuxtApp.payload.prerenderedAt | ||
} |
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 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,19 @@ | ||
import { defineNuxtPlugin, loadPayload, addRouteMiddleware, isPrerendered } from '#app' | ||
|
||
export default defineNuxtPlugin((nuxtApp) => { | ||
// Only enable behavior if initial page is prerendered | ||
// TOOD: Support hybrid | ||
if (!isPrerendered()) { | ||
return | ||
} | ||
addRouteMiddleware(async (to, from) => { | ||
if (to.path === from.path) { return } | ||
const url = to.path | ||
const payload = await loadPayload(url) | ||
if (!payload) { | ||
return | ||
} | ||
Object.assign(nuxtApp.payload.data, payload.data) | ||
Object.assign(nuxtApp.payload.state, payload.state) | ||
}) | ||
}) |
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 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 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 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 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,3 @@ | ||
export function useRandomState (max: number = 100, name = 'default') { | ||
return useState('random:' + name, () => Math.round(Math.random() * max)) | ||
} |
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 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 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,47 @@ | ||
<template> | ||
<div> | ||
<NuxtLink to="/random/a"> | ||
Random (A) | ||
</NuxtLink> | ||
<NuxtLink to="/random/b"> | ||
Random (B) | ||
</NuxtLink> | ||
<NuxtLink to="/random/c"> | ||
Random (C) | ||
</NuxtLink> | ||
<br> | ||
|
||
Random: {{ random }} | ||
|
||
Random: (global) {{ globalRandom }} | ||
|
||
Random page: <b>{{ route.params.id }}</b><br> | ||
|
||
Here are some random numbers for you: | ||
|
||
<ul> | ||
<li v-for="n in randomNumbers" :key="n"> | ||
{{ n }} | ||
</li> | ||
</ul> | ||
<button @click="() => refresh()"> | ||
Give me another set | ||
</button> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
const route = useRoute() | ||
const pageKey = 'rand_' + route.params.id | ||
const { data: randomNumbers, refresh } = await useFetch('/api/random', { key: pageKey as string }) | ||
const random = useRandomState(100, pageKey) | ||
const globalRandom = useRandomState(100) | ||
// TODO: NuxtLink should do this automatically on observed | ||
if (process.client) { | ||
preloadPayload('/random/c') | ||
} | ||
</script> |
Oops, something went wrong.
888bd7c
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.
Great job 👍🏿