Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dev): fix FOUC when swapping out link tag
Assuming we have a CSS file at the following path... ``` /resources/css/app.css ``` When adding a link tag to the DOM manually (i.e. via Server Side Rendering)... ```html <link rel="stylesheet" href="{DEV_SERVER_URL}/resources/css/app.css"> ``` Vite will now watch this file and hot reload changes in the browser for this asset. When we update `app.css` Vite will update the link tag's `href` attribute like so... ```diff - <link rel="stylesheet" href="{DEV_SERVER_URL}/resources/css/app.css?"> + <link rel="stylesheet" href="{DEV_SERVER_URL}/resources/css/app.css?t={TIMESTAMP}"> ``` This is great, but for a split moment there is a flash of unstyled content as the asset has not loaded yet. The browser unloads the styles from the original stylesheet and waits until the new stylesheet has loaded and been parsed before it will show the new stylings. This PR addresses this problem. Now the process is as follows.. First the client adds a second (cloned) link tag to the DOM, leaving the original in place and still loaded by the browser... ```diff <link rel="stylesheet" href="{DEV_SERVER_URL}/resources/css/app.css?"> + <link rel="stylesheet" href="{DEV_SERVER_URL}/resources/css/app.css?t={TIMESTAMP}"> ``` Once the new stylesheet has loaded, the browser will remove the original stylesheet from the DOM, leaving the new _loaded_ stylesheet in the DOM. ```diff - <link rel="stylesheet" href="{DEV_SERVER_URL}/resources/css/app.css?"> <link rel="stylesheet" href="{DEV_SERVER_URL}/resources/css/app.css?t={TIMESTAMP}"> ``` This process then continues for future updates to the CSS file. I have no idea where to start adding a unit test for this. I'm a dumb back-end developer. Send help! Edit: i've added a test. Not sure if it is any good though!
- Loading branch information
1 parent
fba82d0
commit 96f07a4
Showing
4 changed files
with
69 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters