Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/bridge/src/runtime/nitro/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface NuxtRenderHTMLContext {
head: string[]
headAttrs: string[]
bodyAttrs: string[]
bodyPreprend: string[]
bodyPrepend: string[]
body: string[]
bodyAppend: string[]
}
Expand Down Expand Up @@ -208,7 +208,7 @@ export default defineRenderHandler(async (event) => {
ssrContext.styles
]),
bodyAttrs: normalizeChunks([_rendered.meta.bodyAttrs]),
bodyPreprend: normalizeChunks([
bodyPrepend: normalizeChunks([
ssrContext.teleports?.body,
_rendered.meta.bodyScriptsPrepend
]),
Expand All @@ -234,8 +234,8 @@ export default defineRenderHandler(async (event) => {
HEAD_ATTRS: joinAttrs(htmlContext.headAttrs),
HEAD: joinTags(htmlContext.head),
BODY_ATTRS: joinAttrs(htmlContext.bodyAttrs),
BODY_PREPEND: joinTags(htmlContext.bodyPreprend),
APP: _rendered.html + joinTags(htmlContext.bodyAppend)
BODY_PREPEND: joinTags(htmlContext.bodyPrepend),
APP: joinTags(htmlContext.bodyPrepend) + _rendered.html + joinTags(htmlContext.bodyAppend)
}),
statusCode: event.node.res.statusCode,
statusMessage: event.node.res.statusMessage,
Expand Down
3 changes: 2 additions & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export default defineNuxtConfig({
nitro: {
routeRules: {
'/route-rules/spa': { ssr: false }
}
},
plugins: ['plugins/template.ts']
},
bridge: {
meta: true,
Expand Down
7 changes: 7 additions & 0 deletions playground/pages/nitro/template-plugin.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<template>
<div>
Nitro Plugin
</div>
</template>
<script setup>
</script>
11 changes: 11 additions & 0 deletions playground/server/plugins/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook('render:html' as any, (html: any) => {
for (const bodyPart of html.body) {
if (bodyPart.includes('Nitro Plugin')) {
html.bodyPrepend.push('<p>Prepended HTML</p>')
return
}
}
})
})
7 changes: 7 additions & 0 deletions test/bridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ describe('middleware', () => {
})
})

describe('nitro plugins', () => {
it('should prepend a node to the rendered template', async () => {
const html = await $fetch('/nitro/template-plugin')
expect(html).toMatch(/<body\s?>[\n\s]+<p>Prepended HTML<\/p>/)
})
})

describe('dynamic paths', () => {
if (process.env.NUXT_TEST_DEV) {
// TODO:
Expand Down