From fa3454a6d832cf1933c81c5c354802d4723395b7 Mon Sep 17 00:00:00 2001 From: peng <414326615@qq.com> Date: Wed, 21 Feb 2024 10:41:37 +0800 Subject: [PATCH 01/29] feat: add multiple cdn --- packages/astro/src/@types/astro.ts | 6 ++-- .../astro/src/assets/utils/transformToPath.ts | 11 +++++++ .../astro/src/assets/vite-plugin-assets.ts | 31 ++++++++++++++----- .../src/content/vite-plugin-content-assets.ts | 11 +++++-- packages/astro/src/core/app/types.ts | 4 ++- .../src/core/build/plugins/plugin-manifest.ts | 10 ++++-- packages/astro/src/core/config/schema.ts | 4 +-- packages/astro/src/core/create-vite.ts | 10 ++++-- packages/astro/src/core/render/ssr-element.ts | 25 +++++++++------ packages/internal-helpers/src/path.ts | 5 +++ 10 files changed, 89 insertions(+), 28 deletions(-) diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 8a26ebaba6cd..254d32b473a2 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -13,7 +13,7 @@ import type { AddressInfo } from 'node:net'; import type * as rollup from 'rollup'; import type * as vite from 'vite'; import type { RemotePattern } from '../assets/utils/remotePattern.js'; -import type { SerializedSSRManifest } from '../core/app/types.js'; +import type { SerializedSSRManifest, AssetsPrefix } from '../core/app/types.js'; import type { PageBuildData } from '../core/build/types.js'; import type { AstroConfigType } from '../core/config/index.js'; import type { AstroTimer } from '../core/config/timer.js'; @@ -63,7 +63,7 @@ export type { UnresolvedImageTransform, } from '../assets/types.js'; export type { RemotePattern } from '../assets/utils/remotePattern.js'; -export type { SSRManifest } from '../core/app/types.js'; +export type { SSRManifest, AssetsPrefix } from '../core/app/types.js'; export type { AstroCookieGetOptions, AstroCookieSetOptions, @@ -838,7 +838,7 @@ export interface AstroUserConfig { * } * ``` */ - assetsPrefix?: string; + assetsPrefix?: AssetsPrefix; /** * @docs * @name build.serverEntry diff --git a/packages/astro/src/assets/utils/transformToPath.ts b/packages/astro/src/assets/utils/transformToPath.ts index dd8d17fbeaf3..c6d3bd65fad7 100644 --- a/packages/astro/src/assets/utils/transformToPath.ts +++ b/packages/astro/src/assets/utils/transformToPath.ts @@ -4,6 +4,7 @@ import { removeQueryString } from '../../core/path.js'; import { shorthash } from '../../runtime/server/shorthash.js'; import type { ImageTransform } from '../types.js'; import { isESMImportedImage } from './imageKind.js'; +import type { AssetsPrefix } from '../../@types/astro.js'; export function propsToFilename(transform: ImageTransform, hash: string) { let filename = removeQueryString( @@ -33,3 +34,13 @@ export function hashTransform( ); return shorthash(deterministicString(hashFields)); } + +export function getAssetsPrefix(fileType: string, assetsPrefix: AssetsPrefix):string { + if (typeof assetsPrefix === 'string') return assetsPrefix + if (assetsPrefix[fileType]) { + return assetsPrefix[fileType] + } else if (assetsPrefix.defaultAssetsPrefix) { + return assetsPrefix.defaultAssetsPrefix + } + return '' +} \ No newline at end of file diff --git a/packages/astro/src/assets/vite-plugin-assets.ts b/packages/astro/src/assets/vite-plugin-assets.ts index 5a56e76a6838..c249ff4961be 100644 --- a/packages/astro/src/assets/vite-plugin-assets.ts +++ b/packages/astro/src/assets/vite-plugin-assets.ts @@ -1,6 +1,7 @@ import MagicString from 'magic-string'; import type * as vite from 'vite'; import { normalizePath } from 'vite'; +import { getFileExtension } from '@astrojs/internal-helpers/path'; import type { AstroPluginOptions, ImageTransform } from '../@types/astro.js'; import { extendManualChunks } from '../core/build/plugins/util.js'; import { AstroError, AstroErrorData } from '../core/errors/index.js'; @@ -15,7 +16,8 @@ import { VALID_INPUT_FORMATS, VIRTUAL_MODULE_ID, VIRTUAL_SERVICE_ID } from './co import { emitESMImage } from './utils/emitAsset.js'; import { isESMImportedImage } from './utils/imageKind.js'; import { getProxyCode } from './utils/proxy.js'; -import { hashTransform, propsToFilename } from './utils/transformToPath.js'; +import { getAssetsPrefix, hashTransform, propsToFilename } from './utils/transformToPath.js'; +import { isObject } from '../core/util.js'; const resolvedVirtualModuleId = '\0' + VIRTUAL_MODULE_ID; @@ -94,9 +96,15 @@ export default function assets({ } // Rollup will copy the file to the output directory, this refer to this final path, not to the original path - const finalOriginalImagePath = ( - isESMImportedImage(options.src) ? options.src.src : options.src - ).replace(settings.config.build.assetsPrefix || '', ''); + const ESMImportedImageSrc = isESMImportedImage(options.src) ? options.src.src : options.src; + let pf = '' + const fileExtension = getFileExtension(ESMImportedImageSrc) + if (settings.config.build.assetsPrefix && typeof settings.config.build.assetsPrefix === 'string') { + pf = settings.config.build.assetsPrefix + } else if (isObject(settings.config.build.assetsPrefix)) { + pf = getAssetsPrefix(fileExtension, settings.config.build.assetsPrefix) + } + const finalOriginalImagePath = ESMImportedImageSrc.replace(pf, ''); const hash = hashTransform( options, @@ -130,8 +138,10 @@ export default function assets({ // The paths here are used for URLs, so we need to make sure they have the proper format for an URL // (leading slash, prefixed with the base / assets prefix, encoded, etc) - if (settings.config.build.assetsPrefix) { + if (settings.config.build.assetsPrefix && typeof settings.config.build.assetsPrefix === 'string') { return encodeURI(joinPaths(settings.config.build.assetsPrefix, finalFilePath)); + } else if (isObject(settings.config.build.assetsPrefix)) { + return encodeURI(joinPaths(pf, finalFilePath)); } else { return encodeURI(prependForwardSlash(joinPaths(settings.config.base, finalFilePath))); } @@ -148,8 +158,15 @@ export default function assets({ const [full, hash, postfix = ''] = match; const file = this.getFileName(hash); - const prefix = settings.config.build.assetsPrefix - ? appendForwardSlash(settings.config.build.assetsPrefix) + let pf + if (settings.config.build.assetsPrefix && typeof settings.config.build.assetsPrefix === 'string') { + pf = settings.config.build.assetsPrefix + } else if (isObject(settings.config.build.assetsPrefix)) { + const fileExtension = getFileExtension(file) + pf = getAssetsPrefix(fileExtension, settings.config.build.assetsPrefix) + } + const prefix = pf + ? appendForwardSlash(pf) : resolvedConfig.base; const outputFilepath = prefix + normalizePath(file + postfix); diff --git a/packages/astro/src/content/vite-plugin-content-assets.ts b/packages/astro/src/content/vite-plugin-content-assets.ts index 6eeef1c40640..c5f4662bdd76 100644 --- a/packages/astro/src/content/vite-plugin-content-assets.ts +++ b/packages/astro/src/content/vite-plugin-content-assets.ts @@ -1,6 +1,7 @@ import { extname } from 'node:path'; import { pathToFileURL } from 'node:url'; import type { Plugin, Rollup } from 'vite'; +import { getFileExtension } from '@astrojs/internal-helpers/path'; import type { AstroSettings } from '../@types/astro.js'; import { moduleIsTopLevelPage, walkParentInfos } from '../core/build/graph.js'; import { getPageDataByViteID, type BuildInternals } from '../core/build/internal.js'; @@ -19,6 +20,8 @@ import { STYLES_PLACEHOLDER, } from './consts.js'; import { hasContentFlag } from './utils.js'; +import { isObject } from '../core/util.js'; +import { getAssetsPrefix } from '../assets/utils/transformToPath.js'; export function astroContentAssetPropagationPlugin({ mode, @@ -125,8 +128,12 @@ export function astroConfigBuildPlugin( 'build:post': ({ ssrOutputs, clientOutputs, mutate }) => { const outputs = ssrOutputs.flatMap((o) => o.output); const prependBase = (src: string) => { - if (options.settings.config.build.assetsPrefix) { - return joinPaths(options.settings.config.build.assetsPrefix, src); + const { assetsPrefix } = options.settings.config.build + if (assetsPrefix && typeof assetsPrefix === "string") { + return joinPaths(assetsPrefix, src); + } else if (isObject(assetsPrefix)) { + const pf = getAssetsPrefix(getFileExtension(src), assetsPrefix) + return joinPaths(pf, src); } else { return prependForwardSlash(joinPaths(options.settings.config.base, src)); } diff --git a/packages/astro/src/core/app/types.ts b/packages/astro/src/core/app/types.ts index b38f51d64f72..ef448827f4e4 100644 --- a/packages/astro/src/core/app/types.ts +++ b/packages/astro/src/core/app/types.ts @@ -34,6 +34,8 @@ export type SerializedRouteInfo = Omit & { export type ImportComponentInstance = () => Promise; +export type AssetsPrefix = string | Record + export type SSRManifest = { adapterName: string; routes: RouteInfo[]; @@ -42,7 +44,7 @@ export type SSRManifest = { trailingSlash: 'always' | 'never' | 'ignore'; buildFormat: 'file' | 'directory'; compressHTML: boolean; - assetsPrefix?: string; + assetsPrefix?: AssetsPrefix; renderers: SSRLoadedRenderer[]; /** * Map of directive name (e.g. `load`) to the directive script code diff --git a/packages/astro/src/core/build/plugins/plugin-manifest.ts b/packages/astro/src/core/build/plugins/plugin-manifest.ts index 09408e23af36..6935ba57e592 100644 --- a/packages/astro/src/core/build/plugins/plugin-manifest.ts +++ b/packages/astro/src/core/build/plugins/plugin-manifest.ts @@ -9,13 +9,15 @@ import type { SerializedRouteInfo, SerializedSSRManifest, } from '../../app/types.js'; -import { joinPaths, prependForwardSlash } from '../../path.js'; +import { getFileExtension, joinPaths, prependForwardSlash } from '../../path.js'; import { serializeRouteData } from '../../routing/index.js'; import { addRollupInput } from '../add-rollup-input.js'; import { getOutFile, getOutFolder } from '../common.js'; import { cssOrder, mergeInlineCss, type BuildInternals } from '../internal.js'; import type { AstroBuildPlugin } from '../plugin.js'; import type { StaticBuildOptions } from '../types.js'; +import { isObject } from '../../util.js'; +import { getAssetsPrefix } from '../../../assets/utils/transformToPath.js'; const manifestReplace = '@@ASTRO_MANIFEST_REPLACE@@'; const replaceExp = new RegExp(`['"](${manifestReplace})['"]`, 'g'); @@ -165,8 +167,12 @@ function buildManifest( } const prefixAssetPath = (pth: string) => { - if (settings.config.build.assetsPrefix) { + if (typeof settings.config.build.assetsPrefix === "string") { return joinPaths(settings.config.build.assetsPrefix, pth); + } else if (isObject(settings.config.build.assetsPrefix)) { + const fileType = getFileExtension(pth) + const pf = getAssetsPrefix(fileType, settings.config.build.assetsPrefix) + return joinPaths(pf, pth); } else { return prependForwardSlash(joinPaths(settings.config.base, pth)); } diff --git a/packages/astro/src/core/config/schema.ts b/packages/astro/src/core/config/schema.ts index 29c817bce052..c79222d4d614 100644 --- a/packages/astro/src/core/config/schema.ts +++ b/packages/astro/src/core/config/schema.ts @@ -138,7 +138,7 @@ export const AstroConfigSchema = z.object({ .default(ASTRO_CONFIG_DEFAULTS.build.server) .transform((val) => new URL(val)), assets: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.assets), - assetsPrefix: z.string().optional(), + assetsPrefix: z.string().optional().or(z.record(z.string()).optional()), serverEntry: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.serverEntry), redirects: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.build.redirects), inlineStylesheets: z @@ -479,7 +479,7 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: string) { .default(ASTRO_CONFIG_DEFAULTS.build.server) .transform((val) => resolveDirAsUrl(val, fileProtocolRoot)), assets: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.assets), - assetsPrefix: z.string().optional(), + assetsPrefix: z.string().optional().or(z.record(z.string()).optional()), serverEntry: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.serverEntry), redirects: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.build.redirects), inlineStylesheets: z diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 99082b830419..e3f7b350a0af 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -34,6 +34,8 @@ import type { Logger } from './logger/core.js'; import { createViteLogger } from './logger/vite.js'; import { vitePluginMiddleware } from './middleware/vite-plugin.js'; import { joinPaths } from './path.js'; +import { isObject } from './util.js'; +import { getAssetsPrefix } from '../assets/utils/transformToPath.js'; interface CreateViteOptions { settings: AstroSettings; @@ -205,8 +207,12 @@ export async function createVite( const assetsPrefix = settings.config.build.assetsPrefix; if (assetsPrefix) { commonConfig.experimental = { - renderBuiltUrl(filename, { type }) { + renderBuiltUrl(filename, { type, hostType }) { if (type === 'asset') { + if (isObject(assetsPrefix)) { + const pf = getAssetsPrefix(hostType, assetsPrefix) + return joinPaths(pf, filename); + } return joinPaths(assetsPrefix, filename); } }, @@ -309,6 +315,6 @@ function isCommonNotAstro(dep: string): boolean { ); } -function stringifyForDefine(value: string | undefined): string { +function stringifyForDefine(value: string | undefined | object): string { return typeof value === 'string' ? JSON.stringify(value) : 'undefined'; } diff --git a/packages/astro/src/core/render/ssr-element.ts b/packages/astro/src/core/render/ssr-element.ts index 8593dc0bfcc8..a476912bf4e7 100644 --- a/packages/astro/src/core/render/ssr-element.ts +++ b/packages/astro/src/core/render/ssr-element.ts @@ -1,10 +1,17 @@ -import type { SSRElement } from '../../@types/astro.js'; +import type { SSRElement, AssetsPrefix } from '../../@types/astro.js'; import { joinPaths, prependForwardSlash, slash } from '../../core/path.js'; import type { StylesheetAsset } from '../app/types.js'; +import { getFileExtension } from '@astrojs/internal-helpers/path'; +import { isObject } from '../util.js'; +import { getAssetsPrefix } from '../../assets/utils/transformToPath.js'; -export function createAssetLink(href: string, base?: string, assetsPrefix?: string): string { - if (assetsPrefix) { +export function createAssetLink(href: string, base?: string, assetsPrefix?: AssetsPrefix): string { + if (typeof assetsPrefix === "string") { return joinPaths(assetsPrefix, slash(href)); + } else if (isObject(assetsPrefix)) { + const fileType = getFileExtension(href) + const pf = getAssetsPrefix(fileType, assetsPrefix) + return joinPaths(pf, slash(href)); } else if (base) { return prependForwardSlash(joinPaths(base, slash(href))); } else { @@ -15,7 +22,7 @@ export function createAssetLink(href: string, base?: string, assetsPrefix?: stri export function createStylesheetElement( stylesheet: StylesheetAsset, base?: string, - assetsPrefix?: string + assetsPrefix?: AssetsPrefix ): SSRElement { if (stylesheet.type === 'inline') { return { @@ -36,7 +43,7 @@ export function createStylesheetElement( export function createStylesheetElementSet( stylesheets: StylesheetAsset[], base?: string, - assetsPrefix?: string + assetsPrefix?: AssetsPrefix ): Set { return new Set(stylesheets.map((s) => createStylesheetElement(s, base, assetsPrefix))); } @@ -44,7 +51,7 @@ export function createStylesheetElementSet( export function createModuleScriptElement( script: { type: 'inline' | 'external'; value: string }, base?: string, - assetsPrefix?: string + assetsPrefix?: AssetsPrefix ): SSRElement { if (script.type === 'external') { return createModuleScriptElementWithSrc(script.value, base, assetsPrefix); @@ -61,7 +68,7 @@ export function createModuleScriptElement( export function createModuleScriptElementWithSrc( src: string, base?: string, - assetsPrefix?: string + assetsPrefix?: AssetsPrefix ): SSRElement { return { props: { @@ -75,7 +82,7 @@ export function createModuleScriptElementWithSrc( export function createModuleScriptElementWithSrcSet( srces: string[], site?: string, - assetsPrefix?: string + assetsPrefix?: AssetsPrefix ): Set { return new Set( srces.map((src) => createModuleScriptElementWithSrc(src, site, assetsPrefix)) @@ -85,7 +92,7 @@ export function createModuleScriptElementWithSrcSet( export function createModuleScriptsSet( scripts: { type: 'inline' | 'external'; value: string }[], base?: string, - assetsPrefix?: string + assetsPrefix?: AssetsPrefix ): Set { return new Set( scripts.map((script) => createModuleScriptElement(script, base, assetsPrefix)) diff --git a/packages/internal-helpers/src/path.ts b/packages/internal-helpers/src/path.ts index cc9954ef2c04..ef71a0c4f9f1 100644 --- a/packages/internal-helpers/src/path.ts +++ b/packages/internal-helpers/src/path.ts @@ -80,6 +80,11 @@ export function removeFileExtension(path: string) { return idx === -1 ? path : path.slice(0, idx); } +export function getFileExtension(path: string) { + let idx = path.lastIndexOf('.'); + return idx === -1 ? '' : path.slice(idx+1); +} + export function removeQueryString(path: string) { const index = path.lastIndexOf('?'); return index > 0 ? path.substring(0, index) : path; From 12ed2dcc3fcc4a973be25a7f2623b61b58d1933f Mon Sep 17 00:00:00 2001 From: peng <414326615@qq.com> Date: Wed, 21 Feb 2024 15:23:01 +0800 Subject: [PATCH 02/29] add multiple cdn test --- .../test/astro-assets-prefix-mult-cdn.test.js | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 packages/astro/test/astro-assets-prefix-mult-cdn.test.js diff --git a/packages/astro/test/astro-assets-prefix-mult-cdn.test.js b/packages/astro/test/astro-assets-prefix-mult-cdn.test.js new file mode 100644 index 000000000000..14ef1d7e34bd --- /dev/null +++ b/packages/astro/test/astro-assets-prefix-mult-cdn.test.js @@ -0,0 +1,137 @@ +import { expect } from 'chai'; +import * as cheerio from 'cheerio'; +import testAdapter from './test-adapter.js'; +import { loadFixture } from './test-utils.js'; + +const defaultAssetsPrefixRegex = /^https:\/\/example.com\/_astro\/.*/; + +const jsAssetsPrefixRegex = /^https:\/\/js\.example\.com\/_astro\/.*/; + +const cssAssetsPrefixRegex = /^https:\/\/css\.example\.com\/_astro\/.*/; + +const assetsPrefix = { + 'js': 'https://js.example.com', + 'css': 'https://css.example.com', + 'defaultAssetsPrefix': 'https://example.com' +} + +// Asset prefix for CDN support +describe('Assets Prefix Multiple CDN - Static', () => { + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/astro-assets-prefix', + build: { + assetsPrefix, + } + }); + await fixture.build(); + }); + + it('all stylesheets should start with cssAssetPrefix', async () => { + const html = await fixture.readFile('/index.html'); + const $ = cheerio.load(html); + const stylesheets = $('link[rel="stylesheet"]'); + stylesheets.each((i, el) => { + expect(el.attribs.href).to.match(cssAssetsPrefixRegex); + }); + }); + + it('image src start with defaultAssetsPrefix', async () => { + const html = await fixture.readFile('/index.html'); + const $ = cheerio.load(html); + const imgAsset = $('#image-asset'); + expect(imgAsset.attr('src')).to.match(defaultAssetsPrefixRegex); + }); + + it('react component astro-island should import from jsAssetsPrefix', async () => { + const html = await fixture.readFile('/index.html'); + const $ = cheerio.load(html); + const island = $('astro-island'); + expect(island.attr('component-url')).to.match(jsAssetsPrefixRegex); + expect(island.attr('renderer-url')).to.match(jsAssetsPrefixRegex); + }); + + it('import.meta.env.ASSETS_PREFIX works', async () => { + const html = await fixture.readFile('/index.html'); + const $ = cheerio.load(html); + const env = $('#assets-prefix-env'); + expect(env.text()).to.equal(JSON.stringify(assetsPrefix)); + }); + + it('markdown image src start with assetsPrefix', async () => { + const html = await fixture.readFile('/markdown/index.html'); + const $ = cheerio.load(html); + const imgAsset = $('img'); + expect(imgAsset.attr('src')).to.match(defaultAssetsPrefixRegex); + }); + + it('content collections image src start with assetsPrefix', async () => { + const html = await fixture.readFile('/blog/index.html'); + const $ = cheerio.load(html); + const imgAsset = $('img'); + expect(imgAsset.attr('src')).to.match(defaultAssetsPrefixRegex); + }); +}); + + +describe('Assets Prefix Multiple CDN, server', () => { + let app; + + before(async () => { + const fixture = await loadFixture({ + root: './fixtures/astro-assets-prefix', + output: 'server', + adapter: testAdapter(), + build: { + assetsPrefix, + } + }); + await fixture.build(); + app = await fixture.loadTestAdapterApp(); + }); + + it('all stylesheets should start with assetPrefix', async () => { + const request = new Request('http://example.com/custom-base/'); + const response = await app.render(request); + expect(response.status).to.equal(200); + const html = await response.text(); + const $ = cheerio.load(html); + const stylesheets = $('link[rel="stylesheet"]'); + stylesheets.each((i, el) => { + expect(el.attribs.href).to.match(cssAssetsPrefixRegex); + }); + }); + + it('image src start with assetsPrefix', async () => { + const request = new Request('http://example.com/custom-base/'); + const response = await app.render(request); + expect(response.status).to.equal(200); + const html = await response.text(); + const $ = cheerio.load(html); + const imgAsset = $('#image-asset'); + expect(imgAsset.attr('src')).to.match(defaultAssetsPrefixRegex); + }); + + it('react component astro-island should import from assetsPrefix', async () => { + const request = new Request('http://example.com/custom-base/'); + const response = await app.render(request); + expect(response.status).to.equal(200); + const html = await response.text(); + const $ = cheerio.load(html); + const island = $('astro-island'); + expect(island.attr('component-url')).to.match(jsAssetsPrefixRegex); + expect(island.attr('renderer-url')).to.match(jsAssetsPrefixRegex); + }); + + it('markdown optimized image src does not start with assetsPrefix in SSR', async () => { + const request = new Request('http://example.com/custom-base/markdown/'); + const response = await app.render(request); + expect(response.status).to.equal(200); + const html = await response.text(); + const $ = cheerio.load(html); + const imgAsset = $('img'); + expect(imgAsset.attr('src')).to.not.match(defaultAssetsPrefixRegex); + }); +}); From 327d8c2fbedb8bd52247673ca119f2806085e687 Mon Sep 17 00:00:00 2001 From: peng <414326615@qq.com> Date: Thu, 22 Feb 2024 14:12:21 +0800 Subject: [PATCH 03/29] assetsPrefix not empty string --- packages/astro/src/core/render/ssr-element.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/core/render/ssr-element.ts b/packages/astro/src/core/render/ssr-element.ts index a476912bf4e7..4cf2122873fd 100644 --- a/packages/astro/src/core/render/ssr-element.ts +++ b/packages/astro/src/core/render/ssr-element.ts @@ -6,7 +6,7 @@ import { isObject } from '../util.js'; import { getAssetsPrefix } from '../../assets/utils/transformToPath.js'; export function createAssetLink(href: string, base?: string, assetsPrefix?: AssetsPrefix): string { - if (typeof assetsPrefix === "string") { + if (assetsPrefix && typeof assetsPrefix === "string") { return joinPaths(assetsPrefix, slash(href)); } else if (isObject(assetsPrefix)) { const fileType = getFileExtension(href) From 3c0092559055223a3864c52e209f7b4853fa1c3b Mon Sep 17 00:00:00 2001 From: peng <414326615@qq.com> Date: Thu, 22 Feb 2024 14:53:41 +0800 Subject: [PATCH 04/29] add changeset --- .changeset/fluffy-readers-add.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/fluffy-readers-add.md diff --git a/.changeset/fluffy-readers-add.md b/.changeset/fluffy-readers-add.md new file mode 100644 index 000000000000..c4d156e634ef --- /dev/null +++ b/.changeset/fluffy-readers-add.md @@ -0,0 +1,6 @@ +--- +"@astrojs/internal-helpers": minor +"astro": minor +--- + +Add multiple CDN option, `build.assetsPrefix` also add object type for can config multiple CDN assets prefix. From 0ef27ca24ed5a5198cb136bf0e14fcc2a3da99f5 Mon Sep 17 00:00:00 2001 From: peng <414326615@qq.com> Date: Fri, 23 Feb 2024 14:30:24 +0800 Subject: [PATCH 05/29] simplify code --- .../astro/src/assets/utils/transformToPath.ts | 3 ++- .../astro/src/assets/vite-plugin-assets.ts | 21 ++++--------------- .../src/content/vite-plugin-content-assets.ts | 8 +++---- .../src/core/build/plugins/plugin-manifest.ts | 5 +---- packages/astro/src/core/create-vite.ts | 7 +------ packages/astro/src/core/render/ssr-element.ts | 5 +---- 6 files changed, 12 insertions(+), 37 deletions(-) diff --git a/packages/astro/src/assets/utils/transformToPath.ts b/packages/astro/src/assets/utils/transformToPath.ts index c6d3bd65fad7..41f33aed8536 100644 --- a/packages/astro/src/assets/utils/transformToPath.ts +++ b/packages/astro/src/assets/utils/transformToPath.ts @@ -35,7 +35,8 @@ export function hashTransform( return shorthash(deterministicString(hashFields)); } -export function getAssetsPrefix(fileType: string, assetsPrefix: AssetsPrefix):string { +export function getAssetsPrefix(fileType: string, assetsPrefix?: AssetsPrefix):string { + if (!assetsPrefix) return '' if (typeof assetsPrefix === 'string') return assetsPrefix if (assetsPrefix[fileType]) { return assetsPrefix[fileType] diff --git a/packages/astro/src/assets/vite-plugin-assets.ts b/packages/astro/src/assets/vite-plugin-assets.ts index c249ff4961be..8482042faccd 100644 --- a/packages/astro/src/assets/vite-plugin-assets.ts +++ b/packages/astro/src/assets/vite-plugin-assets.ts @@ -17,7 +17,6 @@ import { emitESMImage } from './utils/emitAsset.js'; import { isESMImportedImage } from './utils/imageKind.js'; import { getProxyCode } from './utils/proxy.js'; import { getAssetsPrefix, hashTransform, propsToFilename } from './utils/transformToPath.js'; -import { isObject } from '../core/util.js'; const resolvedVirtualModuleId = '\0' + VIRTUAL_MODULE_ID; @@ -97,13 +96,8 @@ export default function assets({ // Rollup will copy the file to the output directory, this refer to this final path, not to the original path const ESMImportedImageSrc = isESMImportedImage(options.src) ? options.src.src : options.src; - let pf = '' const fileExtension = getFileExtension(ESMImportedImageSrc) - if (settings.config.build.assetsPrefix && typeof settings.config.build.assetsPrefix === 'string') { - pf = settings.config.build.assetsPrefix - } else if (isObject(settings.config.build.assetsPrefix)) { - pf = getAssetsPrefix(fileExtension, settings.config.build.assetsPrefix) - } + const pf = getAssetsPrefix(fileExtension, settings.config.build.assetsPrefix) const finalOriginalImagePath = ESMImportedImageSrc.replace(pf, ''); const hash = hashTransform( @@ -138,9 +132,7 @@ export default function assets({ // The paths here are used for URLs, so we need to make sure they have the proper format for an URL // (leading slash, prefixed with the base / assets prefix, encoded, etc) - if (settings.config.build.assetsPrefix && typeof settings.config.build.assetsPrefix === 'string') { - return encodeURI(joinPaths(settings.config.build.assetsPrefix, finalFilePath)); - } else if (isObject(settings.config.build.assetsPrefix)) { + if (settings.config.build.assetsPrefix) { return encodeURI(joinPaths(pf, finalFilePath)); } else { return encodeURI(prependForwardSlash(joinPaths(settings.config.base, finalFilePath))); @@ -158,13 +150,8 @@ export default function assets({ const [full, hash, postfix = ''] = match; const file = this.getFileName(hash); - let pf - if (settings.config.build.assetsPrefix && typeof settings.config.build.assetsPrefix === 'string') { - pf = settings.config.build.assetsPrefix - } else if (isObject(settings.config.build.assetsPrefix)) { - const fileExtension = getFileExtension(file) - pf = getAssetsPrefix(fileExtension, settings.config.build.assetsPrefix) - } + const fileExtension = getFileExtension(file) + const pf = getAssetsPrefix(fileExtension, settings.config.build.assetsPrefix) const prefix = pf ? appendForwardSlash(pf) : resolvedConfig.base; diff --git a/packages/astro/src/content/vite-plugin-content-assets.ts b/packages/astro/src/content/vite-plugin-content-assets.ts index c5f4662bdd76..2f256b17f7f0 100644 --- a/packages/astro/src/content/vite-plugin-content-assets.ts +++ b/packages/astro/src/content/vite-plugin-content-assets.ts @@ -20,7 +20,6 @@ import { STYLES_PLACEHOLDER, } from './consts.js'; import { hasContentFlag } from './utils.js'; -import { isObject } from '../core/util.js'; import { getAssetsPrefix } from '../assets/utils/transformToPath.js'; export function astroContentAssetPropagationPlugin({ @@ -129,10 +128,9 @@ export function astroConfigBuildPlugin( const outputs = ssrOutputs.flatMap((o) => o.output); const prependBase = (src: string) => { const { assetsPrefix } = options.settings.config.build - if (assetsPrefix && typeof assetsPrefix === "string") { - return joinPaths(assetsPrefix, src); - } else if (isObject(assetsPrefix)) { - const pf = getAssetsPrefix(getFileExtension(src), assetsPrefix) + if (assetsPrefix) { + const fileExtension = getFileExtension(src) + const pf = getAssetsPrefix(fileExtension, assetsPrefix) return joinPaths(pf, src); } else { return prependForwardSlash(joinPaths(options.settings.config.base, src)); diff --git a/packages/astro/src/core/build/plugins/plugin-manifest.ts b/packages/astro/src/core/build/plugins/plugin-manifest.ts index 6935ba57e592..7109aed16d5b 100644 --- a/packages/astro/src/core/build/plugins/plugin-manifest.ts +++ b/packages/astro/src/core/build/plugins/plugin-manifest.ts @@ -16,7 +16,6 @@ import { getOutFile, getOutFolder } from '../common.js'; import { cssOrder, mergeInlineCss, type BuildInternals } from '../internal.js'; import type { AstroBuildPlugin } from '../plugin.js'; import type { StaticBuildOptions } from '../types.js'; -import { isObject } from '../../util.js'; import { getAssetsPrefix } from '../../../assets/utils/transformToPath.js'; const manifestReplace = '@@ASTRO_MANIFEST_REPLACE@@'; @@ -167,9 +166,7 @@ function buildManifest( } const prefixAssetPath = (pth: string) => { - if (typeof settings.config.build.assetsPrefix === "string") { - return joinPaths(settings.config.build.assetsPrefix, pth); - } else if (isObject(settings.config.build.assetsPrefix)) { + if (settings.config.build.assetsPrefix) { const fileType = getFileExtension(pth) const pf = getAssetsPrefix(fileType, settings.config.build.assetsPrefix) return joinPaths(pf, pth); diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index e3f7b350a0af..275cb09cc573 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -34,7 +34,6 @@ import type { Logger } from './logger/core.js'; import { createViteLogger } from './logger/vite.js'; import { vitePluginMiddleware } from './middleware/vite-plugin.js'; import { joinPaths } from './path.js'; -import { isObject } from './util.js'; import { getAssetsPrefix } from '../assets/utils/transformToPath.js'; interface CreateViteOptions { @@ -209,11 +208,7 @@ export async function createVite( commonConfig.experimental = { renderBuiltUrl(filename, { type, hostType }) { if (type === 'asset') { - if (isObject(assetsPrefix)) { - const pf = getAssetsPrefix(hostType, assetsPrefix) - return joinPaths(pf, filename); - } - return joinPaths(assetsPrefix, filename); + return joinPaths(getAssetsPrefix(hostType, assetsPrefix), filename); } }, }; diff --git a/packages/astro/src/core/render/ssr-element.ts b/packages/astro/src/core/render/ssr-element.ts index 4cf2122873fd..69e88b2a370b 100644 --- a/packages/astro/src/core/render/ssr-element.ts +++ b/packages/astro/src/core/render/ssr-element.ts @@ -2,13 +2,10 @@ import type { SSRElement, AssetsPrefix } from '../../@types/astro.js'; import { joinPaths, prependForwardSlash, slash } from '../../core/path.js'; import type { StylesheetAsset } from '../app/types.js'; import { getFileExtension } from '@astrojs/internal-helpers/path'; -import { isObject } from '../util.js'; import { getAssetsPrefix } from '../../assets/utils/transformToPath.js'; export function createAssetLink(href: string, base?: string, assetsPrefix?: AssetsPrefix): string { - if (assetsPrefix && typeof assetsPrefix === "string") { - return joinPaths(assetsPrefix, slash(href)); - } else if (isObject(assetsPrefix)) { + if (assetsPrefix) { const fileType = getFileExtension(href) const pf = getAssetsPrefix(fileType, assetsPrefix) return joinPaths(pf, slash(href)); From 47b3d5f7d1cf14eea21b3856242986c349ff2b38 Mon Sep 17 00:00:00 2001 From: peng <414326615@qq.com> Date: Thu, 29 Feb 2024 16:31:30 +0800 Subject: [PATCH 06/29] change docs --- .changeset/fluffy-readers-add.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/fluffy-readers-add.md b/.changeset/fluffy-readers-add.md index c4d156e634ef..499105cfaf2b 100644 --- a/.changeset/fluffy-readers-add.md +++ b/.changeset/fluffy-readers-add.md @@ -3,4 +3,4 @@ "astro": minor --- -Add multiple CDN option, `build.assetsPrefix` also add object type for can config multiple CDN assets prefix. +Adds the option to pass an object to `build.assetsPrefix`. This allows for the use of multiple CDN prefixes based on the target filetype. From 3a1dcdb3793caba0dc1682ff3cd46366976fb54e Mon Sep 17 00:00:00 2001 From: peng <414326615@qq.com> Date: Thu, 29 Feb 2024 16:35:09 +0800 Subject: [PATCH 07/29] replace getFileExtension with node path.extname --- packages/astro/src/assets/vite-plugin-assets.ts | 8 ++++---- packages/astro/src/content/vite-plugin-content-assets.ts | 3 +-- packages/astro/src/core/build/plugins/plugin-manifest.ts | 5 +++-- packages/astro/src/core/render/ssr-element.ts | 4 ++-- packages/internal-helpers/src/path.ts | 5 ----- 5 files changed, 10 insertions(+), 15 deletions(-) diff --git a/packages/astro/src/assets/vite-plugin-assets.ts b/packages/astro/src/assets/vite-plugin-assets.ts index 8482042faccd..f531270e3b73 100644 --- a/packages/astro/src/assets/vite-plugin-assets.ts +++ b/packages/astro/src/assets/vite-plugin-assets.ts @@ -1,7 +1,7 @@ import MagicString from 'magic-string'; import type * as vite from 'vite'; import { normalizePath } from 'vite'; -import { getFileExtension } from '@astrojs/internal-helpers/path'; +import { extname } from 'node:path'; import type { AstroPluginOptions, ImageTransform } from '../@types/astro.js'; import { extendManualChunks } from '../core/build/plugins/util.js'; import { AstroError, AstroErrorData } from '../core/errors/index.js'; @@ -96,8 +96,8 @@ export default function assets({ // Rollup will copy the file to the output directory, this refer to this final path, not to the original path const ESMImportedImageSrc = isESMImportedImage(options.src) ? options.src.src : options.src; - const fileExtension = getFileExtension(ESMImportedImageSrc) - const pf = getAssetsPrefix(fileExtension, settings.config.build.assetsPrefix) + const fileExtension = extname(ESMImportedImageSrc); + const pf = getAssetsPrefix(fileExtension, settings.config.build.assetsPrefix); const finalOriginalImagePath = ESMImportedImageSrc.replace(pf, ''); const hash = hashTransform( @@ -150,7 +150,7 @@ export default function assets({ const [full, hash, postfix = ''] = match; const file = this.getFileName(hash); - const fileExtension = getFileExtension(file) + const fileExtension = extname(file) const pf = getAssetsPrefix(fileExtension, settings.config.build.assetsPrefix) const prefix = pf ? appendForwardSlash(pf) diff --git a/packages/astro/src/content/vite-plugin-content-assets.ts b/packages/astro/src/content/vite-plugin-content-assets.ts index 2f256b17f7f0..250af4540f4c 100644 --- a/packages/astro/src/content/vite-plugin-content-assets.ts +++ b/packages/astro/src/content/vite-plugin-content-assets.ts @@ -1,7 +1,6 @@ import { extname } from 'node:path'; import { pathToFileURL } from 'node:url'; import type { Plugin, Rollup } from 'vite'; -import { getFileExtension } from '@astrojs/internal-helpers/path'; import type { AstroSettings } from '../@types/astro.js'; import { moduleIsTopLevelPage, walkParentInfos } from '../core/build/graph.js'; import { getPageDataByViteID, type BuildInternals } from '../core/build/internal.js'; @@ -129,7 +128,7 @@ export function astroConfigBuildPlugin( const prependBase = (src: string) => { const { assetsPrefix } = options.settings.config.build if (assetsPrefix) { - const fileExtension = getFileExtension(src) + const fileExtension = extname(src) const pf = getAssetsPrefix(fileExtension, assetsPrefix) return joinPaths(pf, src); } else { diff --git a/packages/astro/src/core/build/plugins/plugin-manifest.ts b/packages/astro/src/core/build/plugins/plugin-manifest.ts index 7109aed16d5b..3192e0839aab 100644 --- a/packages/astro/src/core/build/plugins/plugin-manifest.ts +++ b/packages/astro/src/core/build/plugins/plugin-manifest.ts @@ -1,5 +1,6 @@ import glob from 'fast-glob'; import { fileURLToPath } from 'node:url'; +import { extname } from 'node:path'; import type { OutputChunk } from 'rollup'; import { type Plugin as VitePlugin } from 'vite'; import { runHookBuildSsr } from '../../../integrations/index.js'; @@ -9,7 +10,7 @@ import type { SerializedRouteInfo, SerializedSSRManifest, } from '../../app/types.js'; -import { getFileExtension, joinPaths, prependForwardSlash } from '../../path.js'; +import { joinPaths, prependForwardSlash } from '../../path.js'; import { serializeRouteData } from '../../routing/index.js'; import { addRollupInput } from '../add-rollup-input.js'; import { getOutFile, getOutFolder } from '../common.js'; @@ -167,7 +168,7 @@ function buildManifest( const prefixAssetPath = (pth: string) => { if (settings.config.build.assetsPrefix) { - const fileType = getFileExtension(pth) + const fileType = extname(pth) const pf = getAssetsPrefix(fileType, settings.config.build.assetsPrefix) return joinPaths(pf, pth); } else { diff --git a/packages/astro/src/core/render/ssr-element.ts b/packages/astro/src/core/render/ssr-element.ts index 69e88b2a370b..ffba433c972b 100644 --- a/packages/astro/src/core/render/ssr-element.ts +++ b/packages/astro/src/core/render/ssr-element.ts @@ -1,12 +1,12 @@ +import { extname } from 'node:path'; import type { SSRElement, AssetsPrefix } from '../../@types/astro.js'; import { joinPaths, prependForwardSlash, slash } from '../../core/path.js'; import type { StylesheetAsset } from '../app/types.js'; -import { getFileExtension } from '@astrojs/internal-helpers/path'; import { getAssetsPrefix } from '../../assets/utils/transformToPath.js'; export function createAssetLink(href: string, base?: string, assetsPrefix?: AssetsPrefix): string { if (assetsPrefix) { - const fileType = getFileExtension(href) + const fileType = extname(href) const pf = getAssetsPrefix(fileType, assetsPrefix) return joinPaths(pf, slash(href)); } else if (base) { diff --git a/packages/internal-helpers/src/path.ts b/packages/internal-helpers/src/path.ts index ef71a0c4f9f1..cc9954ef2c04 100644 --- a/packages/internal-helpers/src/path.ts +++ b/packages/internal-helpers/src/path.ts @@ -80,11 +80,6 @@ export function removeFileExtension(path: string) { return idx === -1 ? path : path.slice(0, idx); } -export function getFileExtension(path: string) { - let idx = path.lastIndexOf('.'); - return idx === -1 ? '' : path.slice(idx+1); -} - export function removeQueryString(path: string) { const index = path.lastIndexOf('?'); return index > 0 ? path.substring(0, index) : path; From acd1ec950e328e506df1f6fe08c2655bdabbf02a Mon Sep 17 00:00:00 2001 From: peng <414326615@qq.com> Date: Fri, 1 Mar 2024 16:35:29 +0800 Subject: [PATCH 08/29] Adapt node extname --- .../astro/src/assets/utils/transformToPath.ts | 11 +++--- packages/astro/src/core/create-vite.ts | 3 +- .../test/astro-assets-prefix-mult-cdn.test.js | 38 +++++++++---------- .../astro-assets-prefix/src/pages/index.astro | 2 +- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/packages/astro/src/assets/utils/transformToPath.ts b/packages/astro/src/assets/utils/transformToPath.ts index 41f33aed8536..54872a73460a 100644 --- a/packages/astro/src/assets/utils/transformToPath.ts +++ b/packages/astro/src/assets/utils/transformToPath.ts @@ -36,12 +36,13 @@ export function hashTransform( } export function getAssetsPrefix(fileType: string, assetsPrefix?: AssetsPrefix):string { - if (!assetsPrefix) return '' - if (typeof assetsPrefix === 'string') return assetsPrefix + if (!assetsPrefix) return ''; + if (typeof assetsPrefix === 'string') return assetsPrefix; + fileType = fileType[0] === '.' ? fileType.slice(1) : fileType; if (assetsPrefix[fileType]) { - return assetsPrefix[fileType] + return assetsPrefix[fileType]; } else if (assetsPrefix.defaultAssetsPrefix) { - return assetsPrefix.defaultAssetsPrefix + return assetsPrefix.defaultAssetsPrefix; } - return '' + return ''; } \ No newline at end of file diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 275cb09cc573..56c2711f7a42 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -35,6 +35,7 @@ import { createViteLogger } from './logger/vite.js'; import { vitePluginMiddleware } from './middleware/vite-plugin.js'; import { joinPaths } from './path.js'; import { getAssetsPrefix } from '../assets/utils/transformToPath.js'; +import { isObject } from './util.js'; interface CreateViteOptions { settings: AstroSettings; @@ -311,5 +312,5 @@ function isCommonNotAstro(dep: string): boolean { } function stringifyForDefine(value: string | undefined | object): string { - return typeof value === 'string' ? JSON.stringify(value) : 'undefined'; + return (typeof value === "string" || isObject(value)) ? JSON.stringify(value) : "undefined"; } diff --git a/packages/astro/test/astro-assets-prefix-mult-cdn.test.js b/packages/astro/test/astro-assets-prefix-mult-cdn.test.js index 14ef1d7e34bd..dd412deba0cc 100644 --- a/packages/astro/test/astro-assets-prefix-mult-cdn.test.js +++ b/packages/astro/test/astro-assets-prefix-mult-cdn.test.js @@ -1,4 +1,5 @@ -import { expect } from 'chai'; +import assert from 'node:assert/strict'; +import { before, describe, it } from 'node:test'; import * as cheerio from 'cheerio'; import testAdapter from './test-adapter.js'; import { loadFixture } from './test-utils.js'; @@ -34,7 +35,7 @@ describe('Assets Prefix Multiple CDN - Static', () => { const $ = cheerio.load(html); const stylesheets = $('link[rel="stylesheet"]'); stylesheets.each((i, el) => { - expect(el.attribs.href).to.match(cssAssetsPrefixRegex); + assert.match(el.attribs.href, cssAssetsPrefixRegex) }); }); @@ -42,40 +43,39 @@ describe('Assets Prefix Multiple CDN - Static', () => { const html = await fixture.readFile('/index.html'); const $ = cheerio.load(html); const imgAsset = $('#image-asset'); - expect(imgAsset.attr('src')).to.match(defaultAssetsPrefixRegex); + assert.match(imgAsset.attr('src'), defaultAssetsPrefixRegex); }); it('react component astro-island should import from jsAssetsPrefix', async () => { const html = await fixture.readFile('/index.html'); const $ = cheerio.load(html); const island = $('astro-island'); - expect(island.attr('component-url')).to.match(jsAssetsPrefixRegex); - expect(island.attr('renderer-url')).to.match(jsAssetsPrefixRegex); + assert.match(island.attr('component-url'), jsAssetsPrefixRegex); + assert.match(island.attr('renderer-url'), jsAssetsPrefixRegex); }); it('import.meta.env.ASSETS_PREFIX works', async () => { const html = await fixture.readFile('/index.html'); const $ = cheerio.load(html); const env = $('#assets-prefix-env'); - expect(env.text()).to.equal(JSON.stringify(assetsPrefix)); + assert.equal(env.text(), JSON.stringify(assetsPrefix)); }); it('markdown image src start with assetsPrefix', async () => { const html = await fixture.readFile('/markdown/index.html'); const $ = cheerio.load(html); - const imgAsset = $('img'); - expect(imgAsset.attr('src')).to.match(defaultAssetsPrefixRegex); + const imgAssets = $('img'); + assert.match(imgAssets.attr('src'), defaultAssetsPrefixRegex); }); it('content collections image src start with assetsPrefix', async () => { const html = await fixture.readFile('/blog/index.html'); const $ = cheerio.load(html); const imgAsset = $('img'); - expect(imgAsset.attr('src')).to.match(defaultAssetsPrefixRegex); + assert.match(imgAsset.attr('src'), defaultAssetsPrefixRegex); }); }); - describe('Assets Prefix Multiple CDN, server', () => { let app; @@ -95,43 +95,43 @@ describe('Assets Prefix Multiple CDN, server', () => { it('all stylesheets should start with assetPrefix', async () => { const request = new Request('http://example.com/custom-base/'); const response = await app.render(request); - expect(response.status).to.equal(200); + assert.equal(response.status, 200); const html = await response.text(); const $ = cheerio.load(html); const stylesheets = $('link[rel="stylesheet"]'); stylesheets.each((i, el) => { - expect(el.attribs.href).to.match(cssAssetsPrefixRegex); + assert.match(el.attribs.href, cssAssetsPrefixRegex); }); }); it('image src start with assetsPrefix', async () => { const request = new Request('http://example.com/custom-base/'); const response = await app.render(request); - expect(response.status).to.equal(200); + assert.equal(response.status, 200); const html = await response.text(); const $ = cheerio.load(html); const imgAsset = $('#image-asset'); - expect(imgAsset.attr('src')).to.match(defaultAssetsPrefixRegex); + assert.match(imgAsset.attr('src'), defaultAssetsPrefixRegex); }); it('react component astro-island should import from assetsPrefix', async () => { const request = new Request('http://example.com/custom-base/'); const response = await app.render(request); - expect(response.status).to.equal(200); + assert.equal(response.status, 200); const html = await response.text(); const $ = cheerio.load(html); const island = $('astro-island'); - expect(island.attr('component-url')).to.match(jsAssetsPrefixRegex); - expect(island.attr('renderer-url')).to.match(jsAssetsPrefixRegex); + assert.match(island.attr('component-url'), jsAssetsPrefixRegex); + assert.match(island.attr('renderer-url'), jsAssetsPrefixRegex); }); it('markdown optimized image src does not start with assetsPrefix in SSR', async () => { const request = new Request('http://example.com/custom-base/markdown/'); const response = await app.render(request); - expect(response.status).to.equal(200); + assert.equal(response.status, 200); const html = await response.text(); const $ = cheerio.load(html); const imgAsset = $('img'); - expect(imgAsset.attr('src')).to.not.match(defaultAssetsPrefixRegex); + assert.doesNotMatch(imgAsset.attr('src'), defaultAssetsPrefixRegex); }); }); diff --git a/packages/astro/test/fixtures/astro-assets-prefix/src/pages/index.astro b/packages/astro/test/fixtures/astro-assets-prefix/src/pages/index.astro index 67f6e97fa56c..4158153205f4 100644 --- a/packages/astro/test/fixtures/astro-assets-prefix/src/pages/index.astro +++ b/packages/astro/test/fixtures/astro-assets-prefix/src/pages/index.astro @@ -13,7 +13,7 @@ import Counter from '../components/Counter.jsx'; penguin penguin -

{import.meta.env.ASSETS_PREFIX}

+

{typeof import.meta.env.ASSETS_PREFIX === 'string' ? import.meta.env.ASSETS_PREFIX : JSON.stringify(import.meta.env.ASSETS_PREFIX)}