Repository navigation
feat: support Nuxt 5 #3891
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
Open
benjamincanac
wants to merge
10
commits into
main
Choose a base branch
from
feat/nuxt-v5
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+574
β293
Open
feat: support Nuxt 5 #3891
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e2f2d97
fix: import nitro helpers from `nitropack/runtime`
benjamincanac 3211d0c
feat: support Nuxt 5 server routes
benjamincanac 22fd861
fix: resolve the sqlite path from the Nitro 3 server entry first
benjamincanac 707c750
fix: skip the compat `event.$fetch` for SSR queries on Nuxt 5
benjamincanac 0ea7841
fix(llms): strip the base URL before queuing raw markdown prerender
benjamincanac fe99148
fix: keep the route error in Nuxt 5 content fetches
benjamincanac bcbe74a
feat: support Nuxt 5 in the cloudflare dump handler
benjamincanac fa6b8d9
Merge remote-tracking branch 'origin/main' into feat/nuxt-v5
benjamincanac 2d9518d
fix: fail the Nuxt 5 cloudflare dump when the asset is missing
benjamincanac 3c51878
fix: fail the Nuxt 5 cloudflare dump when the assets binding misses it
benjamincanac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
17 changes: 17 additions & 0 deletions
17
src/features/llms/runtime/server/content-llms.plugin.legacy.ts
This file contains hidden or 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,17 @@ | ||
| import { appendHeader } from 'h3' | ||
| import type { NitroApp } from 'nitropack/types' | ||
| import { defineNitroPlugin } from 'nitropack/runtime' | ||
| import { registerContentLLMSHooks } from './content-llms' | ||
| import type { ContentLLMSHooks } from './content-llms' | ||
|
|
||
| export default defineNitroPlugin((nitroApp: NitroApp) => { | ||
| const prerenderPaths = registerContentLLMSHooks(nitroApp.hooks as unknown as ContentLLMSHooks) | ||
|
|
||
| if (['nitro-prerender', 'nitro-dev'].includes(import.meta.preset as string)) { | ||
| nitroApp.hooks.hook('beforeResponse', (event) => { | ||
| if (event.path === '/') { | ||
| appendHeader(event, 'x-nitro-prerender', Array.from(prerenderPaths)) | ||
| } | ||
| }) | ||
| } | ||
| }) |
118 changes: 15 additions & 103 deletions
118
src/features/llms/runtime/server/content-llms.plugin.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -1,110 +1,22 @@ | ||
| import { appendHeader } from 'h3' | ||
| import { withBase } from 'ufo' | ||
| import type { NitroApp } from 'nitropack/types' | ||
| import type { ContentLLMSCollectionSection } from './utils' | ||
| import { createDocumentGenerator, prepareContentSections } from './utils' | ||
| import type { PageCollectionItemBase } from '@nuxt/content' | ||
| import { defineNitroPlugin } from 'nitropack/runtime' | ||
| // @ts-expect-error - typecheck does not detect queryCollection in imports | ||
| import { queryCollection } from '#imports' | ||
| import type { ModuleOptions } from 'nuxt-llms' | ||
| import { useRuntimeConfig } from 'nuxt/server' | ||
| import { withoutBase } from 'ufo' | ||
| import { registerContentLLMSHooks } from './content-llms' | ||
| import type { ContentLLMSHooks } from './content-llms' | ||
|
|
||
| export default defineNitroPlugin((nitroApp: NitroApp) => { | ||
| const prerenderPaths = new Set<string>() | ||
| nitroApp.hooks.hook('llms:generate', async (event, options) => { | ||
| prepareContentSections(options.sections) | ||
| interface NitroApp { | ||
| hooks: ContentLLMSHooks | ||
| } | ||
|
|
||
| const sectionsToRemove = [] as string[] | ||
| for (const index in options.sections) { | ||
| const section = options.sections[index] as ContentLLMSCollectionSection | ||
| if (!section.contentCollection) { | ||
| continue | ||
| } | ||
|
|
||
| const query = queryCollection(event, section.contentCollection) | ||
| .select('path', 'title', 'seo', 'description') | ||
| .where('path', 'NOT LIKE', '%/.navigation') | ||
|
|
||
| const filters = section.contentFilters || [] | ||
| for (const filter of filters) { | ||
| query.where(filter.field, filter.operator, filter.value) | ||
| } | ||
|
|
||
| const docs = await query.all() as PageCollectionItemBase[] | ||
|
|
||
| if (docs.length === 0 && section.__nuxt_content_auto_generate) { | ||
| sectionsToRemove.push(index) | ||
| continue | ||
| } | ||
|
|
||
| section.links ||= [] | ||
| section.links.push(...docs.map((doc) => { | ||
| return { | ||
| title: doc.title || doc?.seo?.title || '', | ||
| description: doc.description || doc?.seo?.description || '', | ||
| href: getDocumentLink(doc.path, section.contentCollection!, options), | ||
| } | ||
| })) | ||
| } | ||
|
|
||
| // Delete emoty auto generated sections | ||
| sectionsToRemove.reverse().forEach((index) => { | ||
| options.sections.splice(Number(index), 1) | ||
| }) | ||
| }) | ||
|
|
||
| nitroApp.hooks.hook('llms:generate:full', async (event, options, contents) => { | ||
| prepareContentSections(options.sections) | ||
|
|
||
| const generateDocument = await createDocumentGenerator() | ||
| for (const index in options.sections) { | ||
| const section = options.sections[index] as ContentLLMSCollectionSection | ||
| if (!section.contentCollection) { | ||
| // Ignore non content sections | ||
| continue | ||
| } | ||
|
|
||
| const query = queryCollection(event, section.contentCollection) | ||
| .where('path', 'NOT LIKE', '%/.navigation') | ||
|
|
||
| const filters = section.contentFilters || [] | ||
| for (const filter of filters) { | ||
| query.where(filter.field, filter.operator, filter.value) | ||
| } | ||
|
|
||
| const docs = await query.all() | ||
|
|
||
| for (const doc of docs) { | ||
| // @ts-expect-error -- TODO: fix types | ||
| await nitroApp.hooks.callHook('content:llms:generate:document', event, doc, options) | ||
| const markdown = await generateDocument(doc, options) | ||
| contents.push(markdown!) | ||
| } | ||
| } | ||
| }) | ||
| // Nitro 3 plugin, `definePlugin` from `nitro` only returns the function it is given | ||
| export default (nitroApp: NitroApp) => { | ||
| const prerenderPaths = registerContentLLMSHooks(nitroApp.hooks) | ||
|
|
||
| if (['nitro-prerender', 'nitro-dev'].includes(import.meta.preset as string)) { | ||
| nitroApp.hooks.hook('beforeResponse', (event) => { | ||
| if (event.path === '/') { | ||
| appendHeader(event, 'x-nitro-prerender', Array.from(prerenderPaths)) | ||
| nitroApp.hooks.hook('response', (response: Response, event: { req: Request }) => { | ||
| const path = withoutBase(new URL(event.req.url).pathname, useRuntimeConfig().app.baseURL) | ||
| if (path === '/' && prerenderPaths.size) { | ||
| response.headers.append('x-nitro-prerender', Array.from(prerenderPaths).join(', ')) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| function getDocumentLink(link: string, collection: string, options: ModuleOptions) { | ||
| const contentRawMarkdown = (options as unknown as { contentRawMarkdown: false | { rewriteLLMSTxt: boolean, excludeCollections: string[] } })?.contentRawMarkdown | ||
| if (contentRawMarkdown === false || contentRawMarkdown?.rewriteLLMSTxt === false || contentRawMarkdown?.excludeCollections?.includes(collection)) { | ||
| return withBase(link, options.domain) | ||
| } | ||
|
|
||
| link = `/raw${link}.md` | ||
|
|
||
| if (link.endsWith('/.md')) { | ||
| link = link.slice(0, -3) + 'index.md' | ||
| } | ||
|
|
||
| prerenderPaths.add(link) | ||
|
|
||
| return withBase(link, options.domain) | ||
| } | ||
| }) | ||
| } |
This file contains hidden or 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,109 @@ | ||
| import { withBase } from 'ufo' | ||
| import { queryCollection } from '../../../../runtime/server' | ||
| import type { ModuleOptions } from 'nuxt-llms' | ||
| import type { Collections, PageCollectionItemBase } from '@nuxt/content' | ||
| import type { ContentEvent } from '../../../../runtime/internal/event' | ||
| import type { ContentLLMSCollectionSection } from './utils' | ||
| import { createDocumentGenerator, prepareContentSections } from './utils' | ||
|
|
||
| export interface ContentLLMSHooks { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| hook: (name: string, fn: (...args: any[]) => unknown) => unknown | ||
| callHook: (name: string, ...args: unknown[]) => unknown | ||
| } | ||
|
|
||
| /** | ||
| * Add the content collections to the `llms.txt` sections, and return the raw markdown paths to prerender. | ||
| */ | ||
| export function registerContentLLMSHooks(hooks: ContentLLMSHooks) { | ||
| const prerenderPaths = new Set<string>() | ||
| hooks.hook('llms:generate', async (event: ContentEvent, options: ModuleOptions) => { | ||
| prepareContentSections(options.sections) | ||
|
|
||
| const sectionsToRemove = [] as string[] | ||
| for (const index in options.sections) { | ||
| const section = options.sections[index] as ContentLLMSCollectionSection | ||
| if (!section.contentCollection) { | ||
| continue | ||
| } | ||
|
|
||
| const query = queryCollection(event, section.contentCollection as keyof Collections) | ||
| .select('path', 'title', 'seo', 'description') | ||
| .where('path', 'NOT LIKE', '%/.navigation') | ||
|
|
||
| const filters = section.contentFilters || [] | ||
| for (const filter of filters) { | ||
| query.where(filter.field, filter.operator, filter.value) | ||
| } | ||
|
|
||
| const docs = await query.all() as unknown as PageCollectionItemBase[] | ||
|
|
||
| if (docs.length === 0 && section.__nuxt_content_auto_generate) { | ||
| sectionsToRemove.push(index) | ||
| continue | ||
| } | ||
|
|
||
| section.links ||= [] | ||
| section.links.push(...docs.map((doc) => { | ||
| return { | ||
| title: doc.title || doc?.seo?.title || '', | ||
| description: doc.description || doc?.seo?.description || '', | ||
| href: getDocumentLink(doc.path, section.contentCollection!, options), | ||
| } | ||
| })) | ||
| } | ||
|
|
||
| // Delete emoty auto generated sections | ||
| sectionsToRemove.reverse().forEach((index) => { | ||
| options.sections.splice(Number(index), 1) | ||
| }) | ||
| }) | ||
|
|
||
| hooks.hook('llms:generate:full', async (event: ContentEvent, options: ModuleOptions, contents: string[]) => { | ||
| prepareContentSections(options.sections) | ||
|
|
||
| const generateDocument = await createDocumentGenerator() | ||
| for (const index in options.sections) { | ||
| const section = options.sections[index] as ContentLLMSCollectionSection | ||
| if (!section.contentCollection) { | ||
| // Ignore non content sections | ||
| continue | ||
| } | ||
|
|
||
| const query = queryCollection(event, section.contentCollection as keyof Collections) | ||
| .where('path', 'NOT LIKE', '%/.navigation') | ||
|
|
||
| const filters = section.contentFilters || [] | ||
| for (const filter of filters) { | ||
| query.where(filter.field, filter.operator, filter.value) | ||
| } | ||
|
|
||
| const docs = await query.all() as PageCollectionItemBase[] | ||
|
|
||
| for (const doc of docs) { | ||
| await hooks.callHook('content:llms:generate:document', event, doc, options) | ||
| const markdown = await generateDocument(doc, options) | ||
| contents.push(markdown!) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| function getDocumentLink(link: string, collection: string, options: ModuleOptions) { | ||
| const contentRawMarkdown = (options as unknown as { contentRawMarkdown: false | { rewriteLLMSTxt: boolean, excludeCollections: string[] } })?.contentRawMarkdown | ||
| if (contentRawMarkdown === false || contentRawMarkdown?.rewriteLLMSTxt === false || contentRawMarkdown?.excludeCollections?.includes(collection)) { | ||
| return withBase(link, options.domain) | ||
| } | ||
|
|
||
| link = `/raw${link}.md` | ||
|
|
||
| if (link.endsWith('/.md')) { | ||
| link = link.slice(0, -3) + 'index.md' | ||
| } | ||
|
|
||
| prerenderPaths.add(link) | ||
|
|
||
| return withBase(link, options.domain) | ||
| } | ||
|
|
||
| return prerenderPaths | ||
| } |
This file contains hidden or 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,62 @@ | ||
| import { withLeadingSlash } from 'ufo' | ||
| import { stringify } from 'minimark/stringify' | ||
| import { queryCollection } from '../../../../runtime/server' | ||
| import type { Collections, PageCollectionItemBase, ResolvedCollection } from '@nuxt/content' | ||
| import type { MinimarkNode } from '../../../../types/tree' | ||
| import type { ContentEvent } from '../../../../runtime/internal/event' | ||
| import collections from '#content/manifest' | ||
|
|
||
| export interface ContentRawMarkdownConfig { | ||
| contentRawMarkdown: false | { excludeCollections: string[] } | ||
| } | ||
|
|
||
| /** | ||
| * The raw markdown of the page a `/raw/<path>.md` slug points to, or `undefined` when there is none. | ||
| */ | ||
| export async function getRawMarkdown(event: ContentEvent, slug: string | undefined, llmsConfig: ContentRawMarkdownConfig | undefined) { | ||
| if (!slug?.endsWith('.md') || llmsConfig?.contentRawMarkdown === false) { | ||
| return | ||
| } | ||
|
|
||
| let path = withLeadingSlash(slug.replace('.md', '')) | ||
| if (path.endsWith('/index')) { | ||
| path = path.substring(0, path.length - 6) | ||
| } | ||
|
|
||
| const excludeCollections = llmsConfig?.contentRawMarkdown?.excludeCollections || [] | ||
| const _collections = Object.entries(collections as Record<string, ResolvedCollection>) | ||
| .filter(([_key, value]) => value.type === 'page' && !excludeCollections.includes(_key)) | ||
| .map(([key]) => key) as string[] | ||
|
|
||
| let page: PageCollectionItemBase | null = null | ||
| for (const collection of _collections) { | ||
| page = await queryCollection(event, collection as keyof Collections).path(path).first() as PageCollectionItemBase | null | ||
| if (page) { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if (!page) { | ||
| return | ||
| } | ||
|
|
||
| // Add title and description to the top of the page if missing | ||
| if (page.body.value[0]?.[0] !== 'h1') { | ||
| page.body.value.unshift(['blockquote', {}, page.description]) | ||
| page.body.value.unshift(['h1', {}, page.title]) | ||
| } | ||
|
|
||
| // Append related links at the end if present | ||
| const links = (page as unknown as Record<string, unknown>).links || (page.meta as Record<string, unknown>)?.links | ||
| if (Array.isArray(links) && links.length > 0) { | ||
| const linkItems = links | ||
| .filter((link: { label?: string, to?: string }) => link.label && link.to) | ||
| .map((link: { label: string, to: string }) => ['li', {}, ['a', { href: link.to }, link.label]]) | ||
| if (linkItems.length > 0) { | ||
| page.body.value.push(['hr'] as unknown as MinimarkNode) | ||
| page.body.value.push(['ul', {}, ...linkItems] as unknown as MinimarkNode) | ||
| } | ||
| } | ||
|
|
||
| return stringify({ ...page.body, type: 'minimark' }, { format: 'markdown/html' }) | ||
| } | ||
15 changes: 15 additions & 0 deletions
15
src/features/llms/runtime/server/routes/raw/[...slug].md.get.legacy.ts
This file contains hidden or 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,15 @@ | ||
| import { getRouterParams, eventHandler, createError, setHeader } from 'h3' | ||
| import { useRuntimeConfig } from 'nitropack/runtime' | ||
| import { getRawMarkdown } from '../../raw-markdown' | ||
| import type { ContentRawMarkdownConfig } from '../../raw-markdown' | ||
|
|
||
| export default eventHandler(async (event) => { | ||
| const config = useRuntimeConfig(event) | ||
| const markdown = await getRawMarkdown(event, getRouterParams(event)['slug.md'], config.llms as ContentRawMarkdownConfig) | ||
| if (markdown === undefined) { | ||
| throw createError({ statusCode: 404, statusMessage: 'Page not found', fatal: true }) | ||
| } | ||
|
|
||
| setHeader(event, 'Content-Type', 'text/markdown; charset=utf-8') | ||
| return markdown | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
π― Functional Correctness | π‘ Minor | β‘ Quick win
Replace only the trailing
.mdextension.slug.replace('.md', '')removes the first.mdsubstring, not the extension. For a slug such asdocs.md.guide.md, the path becomes/docs.guide.md, so the page lookup fails and the route returns 404. Useslice(0, -3). The check on Line 17 has already confirmed that the slug ends with.md.Proposed fix
π Committable suggestion
π€ Prompt for AI Agents
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.
Skipping here,
slug.replace('.md', '')is carried over unchanged from the current raw route onmain, so I'd rather fix it in its own PR than change the Nitro 2 behaviour in this one.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.
@benjamincanac Understood. Deferring this fix keeps the existing Nitro 2 behavior unchanged in this PR.
Would you like me to open a follow-up issue for the trailing
.mdfix?You are interacting with an AI system.