Skip to content

Polish HTML structure of the response in the res.redirect() function#5167

Merged
bjohansebas merged 6 commits intoexpressjs:masterfrom
Bernice55231:issue/5058
Jan 16, 2026
Merged

Polish HTML structure of the response in the res.redirect() function#5167
bjohansebas merged 6 commits intoexpressjs:masterfrom
Bernice55231:issue/5058

Conversation

@Bernice55231
Copy link
Contributor

@Bernice55231 Bernice55231 commented Apr 16, 2023

Fixed #5058
To prevent the issue of not showing the DOM body in the old-versioned browser or HTTPServer in redirecting method, it is better to have the <!DOCTYPE html>' and <title>` elements in the response body. If we use an old-versioned browser, it may not automatically fulfill the correct HTML file and thus show only plain text on that page if it does not succeed in redirecting.

@kevinsandow
Copy link

Your changes look good to me, you just missed the tests for the redirect body in test/res.redirect.js.

@Bernice55231
Copy link
Contributor Author

Your changes look good to me, you just missed the tests for the redirect body in test/res.redirect.js.

Hi, I have updated the test file, could you review it again? Thank you!

@kevinsandow
Copy link

Looks good now, but I'm not the maintainer.

@bjohansebas
Copy link
Member

Hi @Bernice55231, would you like to resolve the conflicts in this PR?

@UlisesGascon
Copy link
Member

Keep in mind GHSA-qw6h-vgh9-j6wx when handling this conflict to prevent regressions :)

@bjohansebas bjohansebas requested a review from Copilot April 12, 2025 02:47
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

lib/response.js:840

  • The string concatenation split into multiple lines may risk issues with automatic semicolon insertion in JavaScript; consider moving the '+' operator to the end of the preceding line to ensure the concatenation works as intended.
+ '<body><p>' + statuses.message[status] + '. Redirecting to ' + u + '</p></body>'

@bjohansebas bjohansebas added the semver-minor This change is a semver minor label Jan 16, 2026
@bjohansebas bjohansebas self-assigned this Jan 16, 2026
@bjohansebas bjohansebas merged commit 9a3f7ff into expressjs:master Jan 16, 2026
28 checks passed
@bjohansebas
Copy link
Member

thanks @Bernice55231!

Copy link
Member

@jonchurch jonchurch left a comment

Choose a reason for hiding this comment

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

The change itself is fine, but wanted to leave context here for the future.

The justification for this PR is not accurate per the description nor the linked issue.

To be clear, no browser will fail to render the previous html.

The linked issue specifically wanted to allow the title to be seen in case the user's browser was blocking redirects, or if it launched an app and left the redirect page visible.

The title will now be "Found" for the default (302) which isnt much of an improvement over having the URL. But thats related to the issue not so much this PR.

However, since the PR leaves out the <html> tag, this still technically isnt valid HTML5.

But that does not matter, as browsers will render that for you.

@bjohansebas
Copy link
Member

Good catch, I hadn’t realized that the HTML tag was still missing.

@SaekiTominaga
Copy link

Thank you for everyone's cooperation!

However, since the PR leaves out the tag, this still technically isnt valid HTML5.

html element can omit both start and end tags. Therefore, <!DOCTYPE html><head>...</head><body>...</body> is a valid HTML5 (or HTML Living Standard).

However, from an accessibility perspective, it is better to at least include the start tag and set the lang attribute, making it <!DOCTYPE html><html lang=en>....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

5.x semver-minor This change is a semver minor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTML in the res.redirect() method is missing a DOCTYPE and <title> element

8 participants