@netlify/plugin-nextjs 4.41.4 → 4.41.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import type { Context } from 'https://edge.netlify.com'
2
- import { ElementHandlers, HTMLRewriter } from '../vendor/deno.land/x/html_rewriter@v0.1.0-pre.17/index.ts'
2
+ import { ElementHandlers, HTMLRewriter } from '../vendor/deno.land/x/htmlrewriter@v1.0.0/src/index.ts'
3
3
 
4
4
  export interface FetchEventResult {
5
5
  response: Response
@@ -209,41 +209,49 @@ export const buildResponse = async ({
209
209
  headers.set('content-length', String(body.length))
210
210
  return new Response(body, { ...response, headers })
211
211
  }
212
- // This var will hold the contents of the script tag
213
- let buffer = ''
214
- // Create an HTMLRewriter that matches the Next data script tag
215
- const rewriter = new HTMLRewriter()
216
-
217
- if (response.dataTransforms.length > 0) {
218
- rewriter.on('script[id="__NEXT_DATA__"]', {
219
- text(textChunk) {
220
- // Grab all the chunks in the Next data script tag
221
- buffer += textChunk.text
222
- if (textChunk.lastInTextNode) {
223
- try {
224
- // When we have all the data, try to parse it as JSON
225
- const data = JSON.parse(buffer.trim())
226
- // Apply all of the transforms to the props
227
- const props = response.dataTransforms.reduce((prev, transform) => transform(prev), data.props)
228
- // Replace the data with the transformed props
229
- // With `html: true` the input is treated as raw HTML
230
- // @see https://developers.cloudflare.com/workers/runtime-apis/html-rewriter/#global-types
231
- textChunk.replace(JSON.stringify({ ...data, props }), { html: true })
232
- } catch (err) {
233
- console.log('Could not parse', err)
234
- }
235
- } else {
236
- // Remove the chunk after we've appended it to the buffer
237
- textChunk.remove()
238
- }
239
- },
240
- })
241
- }
242
212
 
243
- if (response.elementHandlers.length > 0) {
244
- response.elementHandlers.forEach(([selector, handlers]) => rewriter.on(selector, handlers))
213
+ if (response.dataTransforms.length > 0 && response.elementHandlers.length > 0) {
214
+ const { initHtmlRewriter } = await import('./html-rewriter-wasm.ts')
215
+ await initHtmlRewriter()
216
+
217
+ // This var will hold the contents of the script tag
218
+ let buffer = ''
219
+ // Create an HTMLRewriter that matches the Next data script tag
220
+ const rewriter = new HTMLRewriter()
221
+
222
+ if (response.dataTransforms.length > 0) {
223
+ rewriter.on('script[id="__NEXT_DATA__"]', {
224
+ text(textChunk) {
225
+ // Grab all the chunks in the Next data script tag
226
+ buffer += textChunk.text
227
+ if (textChunk.lastInTextNode) {
228
+ try {
229
+ // When we have all the data, try to parse it as JSON
230
+ const data = JSON.parse(buffer.trim())
231
+ // Apply all of the transforms to the props
232
+ const props = response.dataTransforms.reduce((prev, transform) => transform(prev), data.props)
233
+ // Replace the data with the transformed props
234
+ // With `html: true` the input is treated as raw HTML
235
+ // @see https://developers.cloudflare.com/workers/runtime-apis/html-rewriter/#global-types
236
+ textChunk.replace(JSON.stringify({ ...data, props }), { html: true })
237
+ } catch (err) {
238
+ console.log('Could not parse', err)
239
+ }
240
+ } else {
241
+ // Remove the chunk after we've appended it to the buffer
242
+ textChunk.remove()
243
+ }
244
+ },
245
+ })
246
+ }
247
+
248
+ if (response.elementHandlers.length > 0) {
249
+ response.elementHandlers.forEach(([selector, handlers]) => rewriter.on(selector, handlers))
250
+ }
251
+ return rewriter.transform(response.originResponse)
252
+ } else {
253
+ return response.originResponse
245
254
  }
246
- return rewriter.transform(response.originResponse)
247
255
  }
248
256
  const res = new Response(result.response.body, result.response)
249
257
  request.headers.set('x-nf-next-middleware', 'skip')