Skip to content

Commit

Permalink
feat(generate): use nitro header instead of header link (#1502)
Browse files Browse the repository at this point in the history
Co-authored-by: Sébastien Chopin <seb@nuxtjs.com>
  • Loading branch information
farnabaz and Atinux committed Sep 6, 2022
1 parent 3eba501 commit 96e3fcb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
10 changes: 3 additions & 7 deletions src/runtime/composables/navigation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { hash } from 'ohash'
import { useHead, useCookie } from '#app'
import { useCookie } from '#app'
import type { NavItem, QueryBuilder, QueryBuilderParams } from '../types'
import { jsonStringify } from '../utils/json'
import { withContentBase } from './utils'
import { addPrerenderPath, withContentBase } from './utils'

export const fetchContentNavigation = (queryBuilder?: QueryBuilder | QueryBuilderParams): Promise<Array<NavItem>> => {
let params = queryBuilder
Expand All @@ -14,11 +14,7 @@ export const fetchContentNavigation = (queryBuilder?: QueryBuilder | QueryBuilde

// Add `prefetch` to `<head>` in production
if (!process.dev && process.server) {
useHead({
link: [
{ rel: 'prefetch', href: apiPath }
]
})
addPrerenderPath(apiPath)
}

return $fetch(apiPath, {
Expand Down
10 changes: 3 additions & 7 deletions src/runtime/composables/query.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { joinURL, withLeadingSlash, withoutTrailingSlash } from 'ufo'
import { hash } from 'ohash'
import { useHead, useCookie } from '#app'
import { useCookie } from '#app'
import { createQuery } from '../query/query'
import type { ParsedContent, QueryBuilder, QueryBuilderParams } from '../types'
import { jsonStringify } from '../utils/json'
import { withContentBase } from './utils'
import { addPrerenderPath, withContentBase } from './utils'

/**
* Query fetcher
Expand All @@ -28,11 +28,7 @@ export const createQueryFetch = <T = ParsedContent>(path?: string) => (query: Qu

// Prefetch the query
if (!process.dev && process.server) {
useHead({
link: [
{ rel: 'prefetch', href: apiPath }
]
})
addPrerenderPath(apiPath)
}

return $fetch(apiPath as any, {
Expand Down
13 changes: 12 additions & 1 deletion src/runtime/composables/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { withBase } from 'ufo'
import { useRuntimeConfig } from '#app'
import { useRuntimeConfig, useRequestEvent } from '#app'
import { unwrap, flatUnwrap } from '../markdown-parser/utils/node'

export const withContentBase = (url: string) => withBase(url, '/api/' + useRuntimeConfig().public.content.base)
Expand All @@ -19,3 +19,14 @@ export const useContentDisabled = () => {
// Break app
throw new Error('useContent is only accessible when you are using `documentDriven` mode.')
}

export const addPrerenderPath = (path: string) => {
const event = useRequestEvent()
event.res.setHeader(
'x-nitro-prerender',
[
event.res.getHeader('x-nitro-prerender'),
path
].filter(Boolean).join(',')
)
}

0 comments on commit 96e3fcb

Please sign in to comment.