Skip to content

Commit d4cd121

Browse files
authored
fix: do not escape HTML (#2007)
1 parent e5ddcd2 commit d4cd121

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

cypress/integration/middleware/enhanced.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
describe('Enhanced middleware', () => {
22
it('rewrites the response body', () => {
33
cy.visit('/static')
4-
cy.get('#message').contains('This was static but has been transformed in')
4+
cy.get('#message').contains('This was static (& escaping test &) but has been transformed in')
55
cy.contains("This is an ad that isn't shown by default")
66
})
77

@@ -10,7 +10,7 @@ describe('Enhanced middleware', () => {
1010
expect(response.body).to.have.nested.property('pageProps.showAd', true)
1111
expect(response.body)
1212
.to.have.nested.property('pageProps.message')
13-
.that.includes('This was static but has been transformed in')
13+
.that.includes('This was static (& escaping test &) but has been transformed in')
1414
})
1515
})
1616

@@ -27,13 +27,13 @@ describe('Enhanced middleware', () => {
2727

2828
it('handles uppercase i18n redirects properly ', () => {
2929
cy.visit('/de-DE/static')
30-
cy.get('#message').contains('This was static but has been transformed in')
30+
cy.get('#message').contains('This was static (& escaping test &) but has been transformed in')
3131
cy.contains("This is an ad that isn't shown by default")
3232
})
3333

3434
it('handles lowercase i18n redirects properly ', () => {
3535
cy.visit('/de-de/static')
36-
cy.get('#message').contains('This was static but has been transformed in')
36+
cy.get('#message').contains('This was static (& escaping test &) but has been transformed in')
3737
cy.contains("This is an ad that isn't shown by default")
3838
})
3939
})

demos/middleware/middleware.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export async function middleware(req: NextRequest) {
2424
if (pathname.startsWith('/static')) {
2525
// Unlike NextResponse.next(), this actually sends the request to the origin
2626
const res = await request.next()
27-
const message = `This was static but has been transformed in ${req.geo?.city}`
27+
const message = `This was static (& escaping test &) but has been transformed in ${req.geo?.city}`
2828

2929
// Transform the response HTML and props
3030
res.replaceText('p[id=message]', message)

packages/runtime/src/templates/edge-shared/utils.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ export const buildResponse = async ({
212212
// Apply all of the transforms to the props
213213
const props = response.dataTransforms.reduce((prev, transform) => transform(prev), data.props)
214214
// Replace the data with the transformed props
215-
textChunk.replace(JSON.stringify({ ...data, props }))
215+
// With `html: true` the input is treated as raw HTML
216+
// @see https://developers.cloudflare.com/workers/runtime-apis/html-rewriter/#global-types
217+
textChunk.replace(JSON.stringify({ ...data, props }), { html: true })
216218
} catch (err) {
217219
console.log('Could not parse', err)
218220
}

0 commit comments

Comments
 (0)