From 55788958e0b257bda6467310aa9da8a7503e54cd Mon Sep 17 00:00:00 2001 From: Ahad Birang Date: Mon, 5 Sep 2022 14:58:15 +0200 Subject: [PATCH] feat(generate): use nitro header instead of header link --- src/runtime/composables/navigation.ts | 10 +++------- src/runtime/composables/query.ts | 10 +++------- src/runtime/composables/utils.ts | 13 ++++++++++++- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/runtime/composables/navigation.ts b/src/runtime/composables/navigation.ts index 248e1081c..deb3b56c0 100644 --- a/src/runtime/composables/navigation.ts +++ b/src/runtime/composables/navigation.ts @@ -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> => { let params = queryBuilder @@ -14,11 +14,7 @@ export const fetchContentNavigation = (queryBuilder?: QueryBuilder | QueryBuilde // Add `prefetch` to `` in production if (!process.dev && process.server) { - useHead({ - link: [ - { rel: 'prefetch', href: apiPath } - ] - }) + addPrerenderPath(apiPath) } return $fetch(apiPath, { diff --git a/src/runtime/composables/query.ts b/src/runtime/composables/query.ts index e4d62b87c..c00624232 100644 --- a/src/runtime/composables/query.ts +++ b/src/runtime/composables/query.ts @@ -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 @@ -28,11 +28,7 @@ export const createQueryFetch = (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, { diff --git a/src/runtime/composables/utils.ts b/src/runtime/composables/utils.ts index 4062ed63f..31d273889 100644 --- a/src/runtime/composables/utils.ts +++ b/src/runtime/composables/utils.ts @@ -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) @@ -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(',') + ) +}