Skip to content

Commit

Permalink
feat: gzip streaming with experimentalCompression
Browse files Browse the repository at this point in the history
Closes #77
  • Loading branch information
harlan-zw committed Jan 3, 2024
1 parent 8d6572c commit c111afd
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@nuxt/kit": "^3.9.0",
"chalk": "^5.3.0",
"defu": "^6.1.3",
"h3-compression": "^0.3.2",
"nuxt-site-config": "^2.2.0",
"nuxt-site-config-kit": "^2.2.0",
"ofetch": "^1.3.3",
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,14 @@ declare module 'vue-router' {
}

if (config.experimentalWarmUp)
addServerPlugin(resolve('./runtime/plugins/warm-up'))
addServerPlugin(resolve('./runtime/nitro/plugins/warm-up'))
if (config.experimentalCompression)
addServerPlugin(resolve('./runtime/nitro/plugins/compression'))

// @ts-expect-error untyped
const isNuxtContentDocumentDriven = (!!nuxt.options.content?.documentDriven || config.strictNuxtContentPaths)
if (hasNuxtModule('@nuxt/content')) {
addServerPlugin(resolve('./runtime/plugins/nuxt-content'))
addServerPlugin(resolve('./runtime/nitro/plugins/nuxt-content'))
addServerHandler({
route: '/__sitemap__/nuxt-content-urls.json',
handler: resolve('./runtime/routes/__sitemap__/nuxt-content-urls'),
Expand Down
9 changes: 9 additions & 0 deletions src/runtime/nitro/plugins/compression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useCompressionStream } from 'h3-compression'
import { defineNitroPlugin } from '#imports'

export default defineNitroPlugin((nitro) => {
nitro.hooks.hook('beforeResponse', async (event, response) => {
if (event.context._isSitemap)
await useCompressionStream(event, response)
})
})
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { defu } from 'defu'
import { defineNitroPlugin } from 'nitropack/dist/runtime/plugin'
import type { ParsedContent } from '@nuxt/content/dist/runtime/types'
import type { SitemapUrl } from '../types'
import { useSimpleSitemapRuntimeConfig } from '../utils'
import type { SitemapUrl } from '../../types'
import { useSimpleSitemapRuntimeConfig } from '../../utils'

export default defineNitroPlugin((nitroApp) => {
const { discoverImages, isNuxtContentDocumentDriven } = useSimpleSitemapRuntimeConfig()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineNitroPlugin } from 'nitropack/dist/runtime/plugin'
import { withLeadingSlash } from 'ufo'
import { useSimpleSitemapRuntimeConfig } from '../utils'
import { useSimpleSitemapRuntimeConfig } from '../../utils'

export default defineNitroPlugin((nitroApp) => {
const { sitemaps } = useSimpleSitemapRuntimeConfig()
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/nitro/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../../.nuxt/tsconfig.server.json"
}
1 change: 1 addition & 0 deletions src/runtime/sitemap/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ export async function createSitemap(e: H3Event, definition: SitemapDefinition) {
setHeader(e, 'Content-Type', 'text/xml; charset=UTF-8')
if (!process.dev)
setHeader(e, 'Cache-Control', 'max-age=600, must-revalidate')
e.context._isSitemap = true
return sitemap
}
8 changes: 7 additions & 1 deletion src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,15 @@ export interface ModuleOptions extends SitemapDefinition {
*
* May be implemented by default in a future minor version.
*
* @experimental
* @experimental Will be enabled by default in v5 (if stable)
*/
experimentalWarmUp?: boolean
/**
* Send the Sitemap as a compressed stream supporting gzip, brolti, etc.
*
* @experimental Will be enabled by default in v5 (if stable)
*/
experimentalCompression?: boolean
}

export interface IndexSitemapRemotes {
Expand Down

0 comments on commit c111afd

Please sign in to comment.