Add a way to preload/preconnect injected script/css files #609
Replies: 8 comments 2 replies
-
Related proposal #561 |
Beta Was this translation helpful? Give feedback.
-
As an alternative, here is a middleware that add preloads for CSS files using rehype: import { defineMiddleware } from 'astro:middleware'
import type { Element, Root } from 'hast'
import { rehype } from 'rehype'
import { visit } from 'unist-util-visit'
import { isCssLink } from 'hast-util-is-css-link'
const rehypeInstance = rehype().use(() => (tree: Root) => {
let head: Element | undefined
const links: Element[] = []
visit(tree, 'element', node => {
if (node.tagName === 'head') head = node
if (isCssLink(node))
links.push({
type: 'element',
tagName: 'link',
properties: { rel: 'preload', as: 'style', href: node.properties.href },
children: []
})
})
head?.children.unshift(...links)
})
export const onRequest = defineMiddleware(async (_, next) => {
const response = await next()
if (response.headers.get('content-type') !== 'text/html') return response
const html = await response.text()
const file = await rehypeInstance.process(html)
return new Response(file.toString(), response)
}) |
Beta Was this translation helpful? Give feedback.
-
+1 the bundled css is render blocking so this should be high priority. |
Beta Was this translation helpful? Give feedback.
-
For anyone wondering, there is a way to achieve this. See https://docs.astro.build/en/guides/styling/#url-css-imports
|
Beta Was this translation helpful? Give feedback.
-
If you're using Sass, hot reload stops working. Here's what I did to fix it:
During the build step, Astro replaces the URLs with the generated .css file. |
Beta Was this translation helpful? Give feedback.
-
Just noting that for most generic use cases, this is not necessary. For a browser, there is no difference between: <head>
<link href="/_astro/404.ae8dd206.css" rel="stylesheet">
</head> and <head>
<link rel="preload" as="style" href="/_astro/404.ae8dd206.css" />
<link href="/_astro/404.ae8dd206.css" rel="stylesheet">
</head> With or without a In general, you only need a preload if the resource is not also included in the head, e.g. because it’s something loaded dynamically (via a script for example). It sounds like doing this might still help in the Cloudflare Pages “Early Hints” use case as it appears that feature parses preload tags and use them for that, but otherwise it generally shouldn’t be needed. As mentioned above, when you do want to do this you can use a ---
import scriptUrl from '../some-script.ts?url';
import styleUrl from '../some-style.css?url';
---
<link rel="preload" href={scriptUrl} as="script">
<link rel="preload" href={styleUrl} as="style"> |
Beta Was this translation helpful? Give feedback.
-
Sorry, I deleted my question because I found solution just right after I posted it. Your example doesn't work for me. I must use ?worker&url to get working URL to TS file. But another problem is, that the builded JS file isn't minified. Is some param to force minified version? // EDIT: I have already solved the file minification, using rollupOptions |
Beta Was this translation helpful? Give feedback.
-
Yes, if you don't use is:inline, Astro should handle it properly
…On Mon, 26 May 2025 at 14:24, Chris Swithinbank ***@***.***> wrote:
I need to preload the script because of PageSpeed says that the script is
blocking requests.
the builded JS file isn't minified.
How do you author your script? An Astro-bundled <script> should not be
render blocking and should be minified, so might need to see your code to
understand what you’re doing.
—
Reply to this email directly, view it on GitHub
<#609 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AATH4NYIXNTLEENS4JQP76D3AL2WFAVCNFSM6AAAAABNIVTTC2VHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTGMRXGE3DIOA>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Body
Summary
I want a simple way to
preload
orpreconnect
injected script/css/fonts files.Background & Motivation
It's help to improve performance and web vitals, see:
It's impossible to customize injected script and know path names in advance.
Exemple:
Goals
Proposal
Add an
Astro.injectedScripts
in Runtime API:Layout.astro:
Limitation:
Related to withastro/astro#5302
I try this vite plugin without success : https://github.com/Applelo/vite-plugin-inject-preload
Beta Was this translation helpful? Give feedback.
All reactions