@@ -38,14 +38,30 @@ describe('custom base URL', async () => {
3838 }
3939 } )
4040
41- it ( 'renders font URLs relatively in CSS' , async ( ) => {
41+ it ( 'resolves font URLs in CSS against the base URL ' , async ( ) => {
4242 for ( const provider of providers ) {
4343 const html = await $fetch < string > ( `/foo/providers/${ provider } ` )
44- const cssLink = html . match ( / < l i n k r e l = " s t y l e s h e e t " h r e f = " ( [ ^ " ] + ) " / ) ! [ 1 ] !
45- const css = await $fetch < string > ( cssLink )
46- const fontUrls = css . match ( / u r l \( ( [ ^ ) ] + ) \) / g)
44+
45+ const cssLink = html . match ( / < l i n k r e l = " s t y l e s h e e t " h r e f = " ( [ ^ " ] + ) " / ) ?. [ 1 ]
46+ if ( cssLink ) {
47+ // External stylesheet: font URLs are written relative to the
48+ // stylesheet path so they resolve regardless of the app base URL.
49+ const css = await $fetch < string > ( cssLink )
50+ const fontUrls = css . match ( / u r l \( ( [ ^ ) ] + ) \) / g)
51+ expect ( fontUrls ! . every ( url =>
52+ url ?. includes ( '../_fonts' )
53+ // global (unresolved) font in css from v4 onwards
54+ || url ?. includes ( '/font-global.woff2' ) ,
55+ ) ) . toBeTruthy ( )
56+ continue
57+ }
58+
59+ // Inlined CSS resolves against the document, so font URLs carry the
60+ // base URL prefix.
61+ const inlineCss = Array . from ( html . matchAll ( / < s t y l e [ ^ > ] * > ( [ \s \S ] * ?) < \/ s t y l e > / g) , m => m [ 1 ] ) . join ( '' )
62+ const fontUrls = inlineCss . match ( / u r l \( ( [ ^ ) ] + ) \) / g)
4763 expect ( fontUrls ! . every ( url =>
48- url ?. includes ( '.. /_fonts' )
64+ url ?. includes ( '/foo /_fonts' )
4965 // global (unresolved) font in css from v4 onwards
5066 || url ?. includes ( '/font-global.woff2' ) ,
5167 ) ) . toBeTruthy ( )
0 commit comments