Skip to content

Commit 3551008

Browse files
authored
fix(vite): generate absolute asset urls (#1023)
1 parent 46570eb commit 3551008

File tree

4 files changed

+74
-73
lines changed

4 files changed

+74
-73
lines changed

src/Tempest/Vite/src/TagsResolver/ManifestTagsResolver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use function Tempest\root_path;
1515
use function Tempest\Support\arr;
1616
use function Tempest\Support\str;
17+
use function Tempest\Support\Str\ensure_starts_with;
1718

1819
final readonly class ManifestTagsResolver implements TagsResolver
1920
{
@@ -237,7 +238,7 @@ private function getChunkPath(Chunk $chunk): string
237238

238239
private function getAssetPath(string $path): string
239240
{
240-
return $this->viteConfig->build->buildDirectory . '/' . $path;
241+
return ensure_starts_with($this->viteConfig->build->buildDirectory . '/' . $path, prefix: '/');
241242
}
242243

243244
private function fileToAssetPath(string $file): string

tests/Integration/Vite/ManifestTagsResolverTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function test_resolve_script(): void
3030

3131
$this->assertSame(
3232
expected: [
33-
'<script type="module" src="build/assets/main-YJD4Cw3J.js"></script>',
33+
'<script type="module" src="/build/assets/main-YJD4Cw3J.js"></script>',
3434
],
3535
actual: $resolver->resolveTags(['src/main.ts']),
3636
);
@@ -46,8 +46,8 @@ public function test_resolve_script_with_css(): void
4646

4747
$this->assertSame(
4848
expected: [
49-
'<link rel="stylesheet" href="build/assets/main-DObprJ9K.css" />',
50-
'<script type="module" src="build/assets/main-CK61jJwL.js"></script>',
49+
'<link rel="stylesheet" href="/build/assets/main-DObprJ9K.css" />',
50+
'<script type="module" src="/build/assets/main-CK61jJwL.js"></script>',
5151
],
5252
actual: $resolver->resolveTags(['src/main.ts']),
5353
);
@@ -73,16 +73,16 @@ public function test_resolve_script_with_prefetching(PrefetchStrategy $strategy)
7373

7474
$tags = $resolver->resolveTags(['resources/js/app.js']);
7575

76-
$this->assertContains('<link rel="modulepreload" href="build/assets/index-BSdK3M0e.js" nonce="123" />', $tags);
77-
$this->assertContains('<link rel="stylesheet" href="build/assets/index-B3s1tYeC.css" nonce="123" />', $tags);
78-
$this->assertContains('<script type="module" src="build/assets/app-lliD09ip.js" nonce="123"></script>', $tags);
76+
$this->assertContains('<link rel="modulepreload" href="/build/assets/index-BSdK3M0e.js" nonce="123" />', $tags);
77+
$this->assertContains('<link rel="stylesheet" href="/build/assets/index-B3s1tYeC.css" nonce="123" />', $tags);
78+
$this->assertContains('<script type="module" src="/build/assets/app-lliD09ip.js" nonce="123"></script>', $tags);
7979

8080
if ($strategy !== PrefetchStrategy::NONE) {
8181
$this->assertStringContainsString('nonce="123', $tags[3]);
8282
$this->assertStringContainsString(
8383
needle: <<<JS
84-
[{"rel":"prefetch","fetchpriority":"low","href":"build\/assets\/index-B3s1tYeC.css"},{"rel":"prefetch","fetchpriority":"low","href":"build\/assets\/index-BSdK3M0e.js"},{"rel":"prefetch","fetchpriority":"low","href":"build\/assets\/_plugin-vue_export-helper-DlAUqK2U.js"},{"rel":"prefetch","fetchpriority":"low","href":"build\/assets\/ApplicationLogo-BhIZH06z.js"},{"rel":"prefetch","fetchpriority":"low","href":"build\/assets\/GuestLayout-BY3LC-73.js"},{"rel":"prefetch","fetchpriority":"low","href":"build\/assets\/TextInput-C8CCB_U_.js"},{"rel":"prefetch","fetchpriority":"low","href":"build\/assets\/PrimaryButton-DuXwr-9M.js"},{"rel":"prefetch","fetchpriority":"low","href":"build\/assets\/ConfirmPassword-CDwcgU8E.js"},{"rel":"prefetch","fetchpriority":"low","href":"build\/assets\/ForgotPassword-B0WWE0BO.js"},{"rel":"prefetch","fetchpriority":"low","href":"build\/assets\/Login-DAFSdGSW.js"},{"rel":"prefetch","fetchpriority":"low","href":"build\/assets\/Register-CfYQbTlA.js"},{"rel":"prefetch","fetchpriority":"low","href":"build\/assets\/ResetPassword-BNl7a4X1.js"},{"rel":"prefetch","fetchpriority":"low","href":"build\/assets\/VerifyEmail-CyukB_SZ.js"},{"rel":"prefetch","fetchpriority":"low","href":"build\/assets\/AuthenticatedLayout-DfWF52N1.js"},{"rel":"prefetch","fetchpriority":"low","href":"build\/assets\/Dashboard-DM_LxQy2.js"},{"rel":"prefetch","fetchpriority":"low","href":"build\/assets\/DeleteUserForm-B1oHFaVP.js"},{"rel":"prefetch","fetchpriority":"low","href":"build\/assets\/UpdatePasswordForm-CaeWqGla.js"},{"rel":"prefetch","fetchpriority":"low","href":"build\/assets\/UpdateProfileInformationForm-CJwkYwQQ.js"},{"rel":"prefetch","fetchpriority":"low","href":"build\/assets\/Edit-CYV2sXpe.js"},{"rel":"prefetch","fetchpriority":"low","href":"build\/assets\/Welcome-D_7l79PQ.js"}]
85-
JS,
84+
[{"rel":"prefetch","fetchpriority":"low","href":"\/build\/assets\/index-B3s1tYeC.css"},{"rel":"prefetch","fetchpriority":"low","href":"\/build\/assets\/index-BSdK3M0e.js"},{"rel":"prefetch","fetchpriority":"low","href":"\/build\/assets\/_plugin-vue_export-helper-DlAUqK2U.js"},{"rel":"prefetch","fetchpriority":"low","href":"\/build\/assets\/ApplicationLogo-BhIZH06z.js"},{"rel":"prefetch","fetchpriority":"low","href":"\/build\/assets\/GuestLayout-BY3LC-73.js"},{"rel":"prefetch","fetchpriority":"low","href":"\/build\/assets\/TextInput-C8CCB_U_.js"},{"rel":"prefetch","fetchpriority":"low","href":"\/build\/assets\/PrimaryButton-DuXwr-9M.js"},{"rel":"prefetch","fetchpriority":"low","href":"\/build\/assets\/ConfirmPassword-CDwcgU8E.js"},{"rel":"prefetch","fetchpriority":"low","href":"\/build\/assets\/ForgotPassword-B0WWE0BO.js"},{"rel":"prefetch","fetchpriority":"low","href":"\/build\/assets\/Login-DAFSdGSW.js"},{"rel":"prefetch","fetchpriority":"low","href":"\/build\/assets\/Register-CfYQbTlA.js"},{"rel":"prefetch","fetchpriority":"low","href":"\/build\/assets\/ResetPassword-BNl7a4X1.js"},{"rel":"prefetch","fetchpriority":"low","href":"\/build\/assets\/VerifyEmail-CyukB_SZ.js"},{"rel":"prefetch","fetchpriority":"low","href":"\/build\/assets\/AuthenticatedLayout-DfWF52N1.js"},{"rel":"prefetch","fetchpriority":"low","href":"\/build\/assets\/Dashboard-DM_LxQy2.js"},{"rel":"prefetch","fetchpriority":"low","href":"\/build\/assets\/DeleteUserForm-B1oHFaVP.js"},{"rel":"prefetch","fetchpriority":"low","href":"\/build\/assets\/UpdatePasswordForm-CaeWqGla.js"},{"rel":"prefetch","fetchpriority":"low","href":"\/build\/assets\/UpdateProfileInformationForm-CJwkYwQQ.js"},{"rel":"prefetch","fetchpriority":"low","href":"\/build\/assets\/Edit-CYV2sXpe.js"},{"rel":"prefetch","fetchpriority":"low","href":"\/build\/assets\/Welcome-D_7l79PQ.js"}]
85+
JS,
8686
haystack: $tags[3],
8787
);
8888
}

tests/Integration/Vite/ViteTagsComponentTest.php

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ public function test_dev_entrypoint(): void
3333
));
3434

3535
$html = $this->render(<<<'HTML'
36-
<html lang="en">
37-
<head>
38-
<x-vite-tags entrypoint="src/foo.ts" />
39-
</head>
40-
<body>Foo</body>
41-
</html>
42-
HTML);
36+
<html lang="en">
37+
<head>
38+
<x-vite-tags entrypoint="src/foo.ts" />
39+
</head>
40+
<body>Foo</body>
41+
</html>
42+
HTML);
4343

4444
$this->assertStringEqualsStringIgnoringLineEndings(
4545
expected: <<<HTML
46-
<html lang="en"><head><script type="module" src="http://localhost:5173/@vite/client"></script><script type="module" src="http://localhost:5173/src/foo.ts"></script></head><body>Foo
47-
</body></html>
48-
HTML,
46+
<html lang="en"><head><script type="module" src="http://localhost:5173/@vite/client"></script><script type="module" src="http://localhost:5173/src/foo.ts"></script></head><body>Foo
47+
</body></html>
48+
HTML,
4949
actual: $html,
5050
);
5151
},
@@ -68,19 +68,19 @@ public function test_dev_entrypoints(): void
6868
));
6969

7070
$html = $this->render(<<<'HTML'
71-
<html lang="en">
72-
<head>
73-
<x-vite-tags :entrypoint="['src/foo.ts', 'src/bar.css']" />
74-
</head>
75-
<body>Foo</body>
76-
</html>
77-
HTML);
71+
<html lang="en">
72+
<head>
73+
<x-vite-tags :entrypoint="['src/foo.ts', 'src/bar.css']" />
74+
</head>
75+
<body>Foo</body>
76+
</html>
77+
HTML);
7878

7979
$this->assertStringEqualsStringIgnoringLineEndings(
8080
expected: <<<HTML
81-
<html lang="en"><head><script type="module" src="http://localhost:5173/@vite/client"></script><script type="module" src="http://localhost:5173/src/foo.ts"></script><link rel="stylesheet" href="http://localhost:5173/src/bar.css" /></head><body>Foo
82-
</body></html>
83-
HTML,
81+
<html lang="en"><head><script type="module" src="http://localhost:5173/@vite/client"></script><script type="module" src="http://localhost:5173/src/foo.ts"></script><link rel="stylesheet" href="http://localhost:5173/src/bar.css" /></head><body>Foo
82+
</body></html>
83+
HTML,
8484
actual: $html,
8585
);
8686
},
@@ -103,19 +103,19 @@ public function test_dev_entrypoints_from_config(): void
103103
));
104104

105105
$html = $this->render(<<<'HTML'
106-
<html lang="en">
107-
<head>
108-
<x-vite-tags />
109-
</head>
110-
<body>Foo</body>
111-
</html>
112-
HTML);
106+
<html lang="en">
107+
<head>
108+
<x-vite-tags />
109+
</head>
110+
<body>Foo</body>
111+
</html>
112+
HTML);
113113

114114
$this->assertStringEqualsStringIgnoringLineEndings(
115115
expected: <<<HTML
116-
<html lang="en"><head><script type="module" src="http://localhost:5173/@vite/client"></script><script type="module" src="http://localhost:5173/src/foo.ts"></script><link rel="stylesheet" href="http://localhost:5173/src/bar.css" /></head><body>Foo
117-
</body></html>
118-
HTML,
116+
<html lang="en"><head><script type="module" src="http://localhost:5173/@vite/client"></script><script type="module" src="http://localhost:5173/src/foo.ts"></script><link rel="stylesheet" href="http://localhost:5173/src/bar.css" /></head><body>Foo
117+
</body></html>
118+
HTML,
119119
actual: $html,
120120
);
121121
},
@@ -139,17 +139,17 @@ public function test_production_entrypoint_from_config(): void
139139
));
140140

141141
$html = $this->render(<<<'HTML'
142-
<html lang="en">
143-
<head>
144-
<x-vite-tags />
145-
</head>
146-
<body></body>
147-
</html>
148-
HTML);
142+
<html lang="en">
143+
<head>
144+
<x-vite-tags />
145+
</head>
146+
<body></body>
147+
</html>
148+
HTML);
149149

150150
$this->assertStringEqualsStringIgnoringLineEndings(<<<'HTML'
151-
<html lang="en"><head><script type="module" src="build/assets/foo-YJD4Cw3J.js"></script></head><body></body></html>
152-
HTML, $html);
151+
<html lang="en"><head><script type="module" src="/build/assets/foo-YJD4Cw3J.js"></script></head><body></body></html>
152+
HTML, $html);
153153
},
154154
files: [
155155
'public/build/manifest.json' => $this->fixture('two-unrelated-entrypoints.json'),
@@ -169,17 +169,17 @@ public function test_production_entrypoint(): void
169169
));
170170

171171
$html = $this->render(<<<'HTML'
172-
<html lang="en">
173-
<head>
174-
<x-vite-tags entrypoint="src/bar.ts" />
175-
</head>
176-
<body></body>
177-
</html>
178-
HTML);
172+
<html lang="en">
173+
<head>
174+
<x-vite-tags entrypoint="src/bar.ts" />
175+
</head>
176+
<body></body>
177+
</html>
178+
HTML);
179179

180180
$this->assertStringEqualsStringIgnoringLineEndings(<<<'HTML'
181-
<html lang="en"><head><script type="module" src="build/assets/bar-WlXl03ld.js"></script></head><body></body></html>
182-
HTML, $html);
181+
<html lang="en"><head><script type="module" src="/build/assets/bar-WlXl03ld.js"></script></head><body></body></html>
182+
HTML, $html);
183183
},
184184
files: [
185185
'public/build/manifest.json' => $this->fixture('two-unrelated-entrypoints.json'),
@@ -199,17 +199,17 @@ public function test_production_entrypoints(): void
199199
));
200200

201201
$html = $this->render(<<<'HTML'
202-
<html lang="en">
203-
<head>
204-
<x-vite-tags :entrypoints="['src/bar.ts', 'src/foo.ts']" />
205-
</head>
206-
<body></body>
207-
</html>
208-
HTML);
202+
<html lang="en">
203+
<head>
204+
<x-vite-tags :entrypoints="['src/bar.ts', 'src/foo.ts']" />
205+
</head>
206+
<body></body>
207+
</html>
208+
HTML);
209209

210210
$this->assertStringEqualsStringIgnoringLineEndings(<<<'HTML'
211-
<html lang="en"><head><script type="module" src="build/assets/bar-WlXl03ld.js"></script><script type="module" src="build/assets/foo-YJD4Cw3J.js"></script></head><body></body></html>
212-
HTML, $html);
211+
<html lang="en"><head><script type="module" src="/build/assets/bar-WlXl03ld.js"></script><script type="module" src="/build/assets/foo-YJD4Cw3J.js"></script></head><body></body></html>
212+
HTML, $html);
213213
},
214214
files: [
215215
'public/build/manifest.json' => $this->fixture('two-unrelated-entrypoints.json'),

tests/Integration/Vite/ViteTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function test_get_tags_with_manifest_with_specified_entrypoints(): void
9494

9595
$this->assertSame(
9696
expected: [
97-
'<script type="module" src="build/assets/main-YJD4Cw3J.js"></script>',
97+
'<script type="module" src="/build/assets/main-YJD4Cw3J.js"></script>',
9898
],
9999
actual: $tags,
100100
);
@@ -130,8 +130,8 @@ public function test_get_tags_with_manifest_with_css(): void
130130

131131
$this->assertSame(
132132
expected: [
133-
'<link rel="stylesheet" href="build/assets/main-DObprJ9K.css" />',
134-
'<script type="module" src="build/assets/main-CK61jJwL.js"></script>',
133+
'<link rel="stylesheet" href="/build/assets/main-DObprJ9K.css" />',
134+
'<script type="module" src="/build/assets/main-CK61jJwL.js"></script>',
135135
],
136136
actual: $tags,
137137
);
@@ -151,9 +151,9 @@ public function test_get_tags_with_manifest_and_preloading(): void
151151

152152
$this->assertSame(
153153
expected: [
154-
'<link rel="modulepreload" href="build/assets/index-BSdK3M0e.js" />',
155-
'<link rel="stylesheet" href="build/assets/index-B3s1tYeC.css" />',
156-
'<script type="module" src="build/assets/app-lliD09ip.js"></script>',
154+
'<link rel="modulepreload" href="/build/assets/index-BSdK3M0e.js" />',
155+
'<link rel="stylesheet" href="/build/assets/index-B3s1tYeC.css" />',
156+
'<script type="module" src="/build/assets/app-lliD09ip.js"></script>',
157157
],
158158
actual: $tags,
159159
);
@@ -179,7 +179,7 @@ public function test_get_tags_with_entrypoints_and_global_entrypoints(): void
179179
$tags = $vite->getTags(['src/bar.ts']);
180180

181181
$this->assertSame(
182-
expected: ['<script type="module" src="build/assets/bar-WlXl03ld.js"></script>'],
182+
expected: ['<script type="module" src="/build/assets/bar-WlXl03ld.js"></script>'],
183183
actual: $tags,
184184
);
185185
},

0 commit comments

Comments
 (0)