Skip to content
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

feat(generate): use nitro header instead of header link #1502

Merged
merged 2 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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(',')
)
}