Skip to content
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

Set the correct content-type for CSS HMR #3459

Merged
merged 2 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/fifty-mice-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Forces the correct mime type for CSS in HMR
28 changes: 28 additions & 0 deletions packages/astro/src/vite-plugin-astro-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,31 @@ async function handleRequest(
}
}

/**
* Vite HMR sends requests for new CSS and those get returned as JS, but we want it to be CSS
* since they are inside of a link tag for Astro.
*/
const forceTextCSSForStylesMiddleware: vite.Connect.NextHandleFunction = function(req, res, next) {
if(req.url) {
// We are just using this to parse the URL to get the search params object
// so the second arg here doesn't matter
const url = new URL(req.url, 'https://astro.build');
// lang.css is a search param that exists on Astro, Svelte, and Vue components.
// We only want to override for astro files.
if(url.searchParams.has('astro') && url.searchParams.has('lang.css')) {
// Override setHeader so we can set the correct content-type for this request.
const setHeader = res.setHeader;
res.setHeader = function(key, value) {
if(key.toLowerCase() === 'content-type') {
return setHeader.call(this, key, 'text/css');
}
return setHeader.apply(this, [key, value]);
};
}
}
next();
};

export default function createPlugin({ config, logging }: AstroPluginOptions): vite.Plugin {
return {
name: 'astro:server',
Expand All @@ -354,6 +379,9 @@ export default function createPlugin({ config, logging }: AstroPluginOptions): v
viteServer.watcher.on('change', rebuildManifest.bind(null, false));
return () => {
removeViteHttpMiddleware(viteServer.middlewares);

// Push this middleware to the front of the stack so that it can intercept responses.
viteServer.middlewares.stack.unshift({ route: '', handle: forceTextCSSForStylesMiddleware });
viteServer.middlewares.use(async (req, res) => {
if (!req.url || !req.method) {
throw new Error('Incomplete request');
Expand Down
11 changes: 11 additions & 0 deletions packages/astro/test/fixtures/hmr-css/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<title>Testing</title>
<style>
background { background: brown; }
</style>
</head>
<body>
<h1>Testing</h1>
</body>
</html>
29 changes: 29 additions & 0 deletions packages/astro/test/hmr-css.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { isWindows, loadFixture } from './test-utils.js';
import { expect } from 'chai';
import * as cheerio from 'cheerio';

describe('HMR - CSS', () => {
if (isWindows) return;

/** @type {import('./test-utils').Fixture} */
let fixture;
/** @type {import('./test-utils').DevServer} */
let devServer;

before(async () => {
fixture = await loadFixture({
root: './fixtures/hmr-css/',
});
devServer = await fixture.startDevServer();
});

after(async () => {
await devServer.stop();
});

it('Timestamp URL used by Vite gets the right mime type', async () => {
let res = await fixture.fetch('/src/pages/index.astro?astro=&type=style&index=0&lang.css=&t=1653657441095');
let headers = res.headers;
expect(headers.get('content-type')).to.equal('text/css');
});
});