Skip to content

Commit 36a461b

Browse files
feat: stabilize experimental preserveScriptOrder option (#14480)
1 parent 9fdfd4c commit 36a461b

File tree

8 files changed

+59
-60
lines changed

8 files changed

+59
-60
lines changed

.changeset/whole-geckos-think.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
'astro': major
3+
---
4+
5+
Updates `<script>` and `<style>` tags to render in the order they are defined
6+
7+
In Astro v5.5, the `experimental.preserveScriptOrder` flag was introduced to render multiple `<style>` and `<script>` tags in the same order as they were declared in the source code. Astro 5.x reversed their order in your generated HTML output. This could give unexpected results, for example, CSS styles being overridden by earlier defined style tags when your site was built.
8+
9+
Astro 6.0 removes this experimental flag and makes this the new default behavior in Astro: scripts and styles are now rendered in the order defined in your code.
10+
11+
#### What should I do?
12+
13+
If you were previously using this experimental feature, you must remove this experimental flag from your configuration as it no longer exists:
14+
15+
```diff
16+
// astro.config.mjs
17+
import { defineConfig } from 'astro/config';
18+
19+
export default defineConfig({
20+
experimental: {
21+
- preserveScriptOrder: true,
22+
},
23+
})
24+
```
25+
26+
Review your `<script>` and `<style>` tags to make sure they behave as desired. You may need to reverse their order:
27+
28+
```diff
29+
<!-- src/components/MyComponent.astro -->
30+
<p>I am a component</p>
31+
<style>
32+
body {
33+
- background: red;
34+
+ background: yellow;
35+
}
36+
</style>
37+
<style>
38+
body {
39+
- background: yellow;
40+
+ background: red;
41+
}
42+
</style>
43+
<script>
44+
- console.log("hello")
45+
+ console.log("world")
46+
</script>
47+
<script>
48+
- console.log("world!")
49+
+ console.log("hello!")
50+
</script>
51+
```

packages/astro/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
"test:integration": "astro-scripts test \"test/*.test.js\""
107107
},
108108
"dependencies": {
109-
"@astrojs/compiler": "0.0.0-next-result-create-astro-20250926081949",
109+
"@astrojs/compiler": "0.0.0-script-order-20251002140654",
110110
"@astrojs/internal-helpers": "workspace:*",
111111
"@astrojs/markdown-remark": "workspace:*",
112112
"@astrojs/telemetry": "workspace:*",

packages/astro/src/core/compile/compile.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ export async function compile({
6262
cssPartialCompileResults,
6363
cssTransformErrors,
6464
}),
65-
experimentalScriptOrder: astroConfig.experimental.preserveScriptOrder ?? false,
6665
async resolvePath(specifier) {
6766
return resolvePath(specifier, filename);
6867
},

packages/astro/src/core/config/schemas/base.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ export const ASTRO_CONFIG_DEFAULTS = {
9797
clientPrerender: false,
9898
contentIntellisense: false,
9999
headingIdCompat: false,
100-
preserveScriptOrder: false,
101100
liveContentCollections: false,
102101
csp: false,
103102
staticImportMetaEnv: false,
@@ -468,10 +467,6 @@ export const AstroConfigSchema = z.object({
468467
.boolean()
469468
.optional()
470469
.default(ASTRO_CONFIG_DEFAULTS.experimental.headingIdCompat),
471-
preserveScriptOrder: z
472-
.boolean()
473-
.optional()
474-
.default(ASTRO_CONFIG_DEFAULTS.experimental.preserveScriptOrder),
475470
fonts: z.array(z.union([localFontFamilySchema, remoteFontFamilySchema])).optional(),
476471
liveContentCollections: z
477472
.boolean()

packages/astro/src/types/public/config.ts

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,48 +2358,6 @@ export interface ViteUserConfig extends OriginalViteUserConfig {
23582358
directives?: CspDirective[];
23592359
};
23602360

2361-
/**
2362-
* @name experimental.preserveScriptOrder
2363-
* @type {boolean}
2364-
* @default `false`
2365-
* @version 5.5
2366-
* @description
2367-
*
2368-
* When enabled, `<script>` and `<style>` tags are rendered in the same order as they are defined.
2369-
*
2370-
* ## Example
2371-
*
2372-
* Consider the following component:
2373-
*
2374-
* ```html
2375-
* <p>I am a component</p>
2376-
* <style>
2377-
* body {
2378-
* background: red;
2379-
* }
2380-
* </style>
2381-
* <style>
2382-
* body {
2383-
* background: yellow;
2384-
* }
2385-
* </style>
2386-
* ```
2387-
*
2388-
* By default, it will generate a CSS style where `red` will be applied:
2389-
*
2390-
* ```css
2391-
* body {background:#ff0} body {background:red}
2392-
* ```
2393-
*
2394-
* When this new option is set to `true`, the generated CSS style will apply `yellow`:
2395-
*
2396-
* ```css
2397-
* body {background:red} body {background:#ff0}
2398-
* ```
2399-
*
2400-
*/
2401-
preserveScriptOrder?: boolean;
2402-
24032361
/**
24042362
* @name experimental.liveContentCollections
24052363
* @type {boolean}

packages/astro/test/astro-directives.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('Directives', async () => {
7070
$('#compound-style')
7171
.attr('style')
7272
.toString()
73-
.includes('color:var(--fg);--fg: black;--bg: white;'),
73+
.includes('color:var(--fg);--bg: white;--fg: black;'),
7474
true,
7575
);
7676
});
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
import { defineConfig } from 'astro/config';
22

3-
export default defineConfig({
4-
experimental: {
5-
preserveScriptOrder: true,
6-
}
7-
});
3+
export default defineConfig({});

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)