Skip to content

Commit

Permalink
revert: asset feature in script
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca committed Sep 26, 2023
1 parent e3cf84f commit 2647487
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 153 deletions.
140 changes: 2 additions & 138 deletions src/utils/render-attributes/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe("utils", () => {
);
});

it('should add the assetPrefix to the "src" attribute when the asset attribute is in production', () => {
it('should add the assetPrefix to the "src" attribute for internal src (PRODUCTION)', () => {
globalThis.mockConstants = {
...(getConstants() ?? {}),
IS_PRODUCTION: true,
Expand Down Expand Up @@ -225,7 +225,7 @@ describe("utils", () => {
);
});

it('should NOT add the assetPrefix to the "src" attribute when the asset attribute is in development', () => {
it('should NOT add the assetPrefix to the "src" attribute for internal src (DEVELOPMENT)', () => {
globalThis.mockConstants = {
...(getConstants() ?? {}),
IS_PRODUCTION: false,
Expand Down Expand Up @@ -265,141 +265,5 @@ describe("utils", () => {

expect(scriptSrc("/some-script.js")).toBe(' src="/some-script.js"');
});

it('should move to assets the "script" src attribute when the asset attribute is in production', () => {
globalThis.mockConstants = {
...(getConstants() ?? {}),
IS_PRODUCTION: true,
};

const request = new RequestContext(new Request("https://example.com"));

const srcOfScriptTag = (src: string) =>
renderAttributes({
props: {
src,
asset: true,
},
request,
type: "script",
});

expect(srcOfScriptTag("https://example.com/some-script.js")).toBe(
' src="/_scripts/some-script.js"',
);
expect(srcOfScriptTag("https://example.com/some-script.js?foo=bar")).toBe(
' src="/_scripts/some-script.js?foo=bar"',
);
expect(srcOfScriptTag("https://example.com/some-script.js#foo")).toBe(
' src="/_scripts/some-script.js#foo"',
);
expect(
srcOfScriptTag("https://example.com/some-script.js?foo=bar#foo"),
).toBe(' src="/_scripts/some-script.js?foo=bar#foo"');
expect(srcOfScriptTag("/some-script.js")).toBe(' src="/some-script.js"');
});

it('should move to assets the "script" src attribute with the correct assetPrefix when the asset attribute is in production', () => {
globalThis.mockConstants = {
...(getConstants() ?? {}),
IS_PRODUCTION: true,
CONFIG: {
assetPrefix: "https://cdn.test.com",
},
};

const request = new RequestContext(new Request("https://example.com"));

const srcOfScriptTag = (src: string) =>
renderAttributes({
props: {
src,
asset: true,
},
request,
type: "script",
});

expect(srcOfScriptTag("https://example.com/some-script.js")).toBe(
' src="https://cdn.test.com/_scripts/some-script.js"',
);
expect(srcOfScriptTag("https://example.com/some-script.js?foo=bar")).toBe(
' src="https://cdn.test.com/_scripts/some-script.js?foo=bar"',
);
expect(srcOfScriptTag("https://example.com/some-script.js#foo")).toBe(
' src="https://cdn.test.com/_scripts/some-script.js#foo"',
);
expect(
srcOfScriptTag("https://example.com/some-script.js?foo=bar#foo"),
).toBe(' src="https://cdn.test.com/_scripts/some-script.js?foo=bar#foo"');
expect(srcOfScriptTag("/some-script.js")).toBe(
' src="https://cdn.test.com/some-script.js"',
);
});

it('should NOT move to assets the "script" src attribute WITHOUT the asset attribute, in production', () => {
globalThis.mockConstants = {
...(getConstants() ?? {}),
IS_PRODUCTION: true,
};

const request = new RequestContext(new Request("https://example.com"));

const srcOfScriptTag = (src: string) =>
renderAttributes({
props: {
src,
},
request,
type: "script",
});

expect(srcOfScriptTag("https://example.com/some-script.js")).toBe(
' src="https://example.com/some-script.js"',
);
expect(srcOfScriptTag("https://example.com/some-script.js?foo=bar")).toBe(
' src="https://example.com/some-script.js?foo=bar"',
);
expect(srcOfScriptTag("https://example.com/some-script.js#foo")).toBe(
' src="https://example.com/some-script.js#foo"',
);
expect(
srcOfScriptTag("https://example.com/some-script.js?foo=bar#foo"),
).toBe(' src="https://example.com/some-script.js?foo=bar#foo"');
expect(srcOfScriptTag("/some-script.js")).toBe(' src="/some-script.js"');
});

it('should NOT move to assets the "script" src attribute when the asset attribute is in development', () => {
globalThis.mockConstants = {
...(getConstants() ?? {}),
IS_PRODUCTION: false,
};

const request = new RequestContext(new Request("https://example.com"));

const srcOfScriptTag = (src: string) =>
renderAttributes({
props: {
src,
asset: true,
},
request,
type: "script",
});

expect(srcOfScriptTag("https://example.com/some-script.js")).toBe(
' src="https://example.com/some-script.js"',
);
expect(srcOfScriptTag("https://example.com/some-script.js?foo=bar")).toBe(
' src="https://example.com/some-script.js?foo=bar"',
);
expect(srcOfScriptTag("https://example.com/some-script.js#foo")).toBe(
' src="https://example.com/some-script.js#foo"',
);
expect(
srcOfScriptTag("https://example.com/some-script.js?foo=bar#foo"),
).toBe(' src="https://example.com/some-script.js?foo=bar#foo"');
expect(srcOfScriptTag("/some-script.js")).toBe(' src="/some-script.js"');
});
});
});
15 changes: 0 additions & 15 deletions src/utils/render-attributes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,6 @@ export default function renderAttributes({
let value = props[prop];

if (prop === "children" || (type === "html" && prop === "lang")) continue;
if (prop === "asset" && type === "script") continue;

// Use assetPrefix in production in scripts with "asset" attribute
if (
IS_PRODUCTION &&
prop === "src" &&
type === "script" &&
"asset" in props &&
URL.canParse(value as string)
) {
const url = new URL(value as string);
value = `${CONFIG.assetPrefix}/_scripts${
url.pathname + url.search + url.hash
}`;
}

// Add the assetPrefix to internal assets (img, picture, video, audio, script)
if (
Expand Down

0 comments on commit 2647487

Please sign in to comment.