-
Notifications
You must be signed in to change notification settings - Fork 91
Description
Problem
*.xhtml files return a Content-Type header of text/html; charset=UTF-8 instead of application/xhtml+xml.
Returning the incorrect content-type breaks special features of xhtml documents, like styling with @namespace.
Steps to Reproduce
- Create an xhtml file, e.g.
example.xhtml:<?xml version="1.0" encoding="utf-8"?> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" > <head> <style> @charset "utf-8"; @namespace epub "http://www.idpf.org/2007/ops"; h1[epub|type="title"] { color: blue; } </style> </head> <body epub:type="bodymatter"> <h1 epub:type="title">Hello, world!</h1> </body> </html>
- Open live preview
- Open devtools pane in preview
- Go to the Network tab and inspect Response headers for
example.xhtml
Expected
Content-Typeisapplication/xhtml+xml.- Title is displayed in blue.
Actual
Content-Typeistext/html; charset=UTF-8.- Title is not colored (HTML doesn’t support
@namespacestyles)
Likely Cause
contentLoader.ts overrides the inferred contentType of application/xhtml+xml if the XHTML file has a language ID of "HTML". But there isn’t a separate language syntax for XHTML, so this assumption seems to be incorrect.
vscode-livepreview/src/server/serverUtils/contentLoader.ts
Lines 302 to 305 in ea19805
| if (workspaceDocuments[i].languageId == 'html') { | |
| fileContents = this._injectIntoFile(fileContents); | |
| contentType = 'text/html'; | |
| } |
Workaround
Set .xhtml files’ language type to XML and refresh. Live preview then returns expected Content-Type of application/xhtml+xml. However this breaks code injection, so edits are no longer live-updated.
Suggested solution
if (workspaceDocuments[i].languageId == 'html') {
fileContents = this._injectIntoFile(fileContents);
- contentType = 'text/html';
+ contentType = contentType.includes('html') ? contentType : 'text/html';
}