Skip to content

fix: do not escape HTML #2007

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

Merged
merged 5 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 cypress/integration/middleware/enhanced.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe('Enhanced middleware', () => {
it('rewrites the response body', () => {
cy.visit('/static')
cy.get('#message').contains('This was static but has been transformed in')
cy.get('#message').contains('This was static & old but has been transformed in')
cy.contains("This is an ad that isn't shown by default")
})

Expand All @@ -10,7 +10,7 @@ describe('Enhanced middleware', () => {
expect(response.body).to.have.nested.property('pageProps.showAd', true)
expect(response.body)
.to.have.nested.property('pageProps.message')
.that.includes('This was static but has been transformed in')
.that.includes('This was static & old but has been transformed in')
})
})

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

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

it('handles lowercase i18n redirects properly ', () => {
cy.visit('/de-de/static')
cy.get('#message').contains('This was static but has been transformed in')
cy.get('#message').contains('This was static & old but has been transformed in')
cy.contains("This is an ad that isn't shown by default")
})
})
2 changes: 1 addition & 1 deletion demos/middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function middleware(req: NextRequest) {
if (pathname.startsWith('/static')) {
// Unlike NextResponse.next(), this actually sends the request to the origin
const res = await request.next()
const message = `This was static but has been transformed in ${req.geo?.city}`
const message = `This was static & old but has been transformed in ${req.geo?.city}`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are great. I wonder if it makes sense to test that & would render as the text &? The only use case for that that I can think of is if a code example was in a prop being set. Maybe not worth it. 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!


// Transform the response HTML and props
res.replaceText('p[id=message]', message)
Expand Down
4 changes: 3 additions & 1 deletion packages/runtime/src/templates/edge-shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ export const buildResponse = async ({
// Apply all of the transforms to the props
const props = response.dataTransforms.reduce((prev, transform) => transform(prev), data.props)
// Replace the data with the transformed props
textChunk.replace(JSON.stringify({ ...data, props }))
// With `html: true` the input is treated as raw HTML
// @see https://developers.cloudflare.com/workers/runtime-apis/html-rewriter/#global-types
textChunk.replace(JSON.stringify({ ...data, props }), { html: true })
} catch (err) {
console.log('Could not parse', err)
}
Expand Down