Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Theme] Fix local asset serve for backward compatibility #4624

Merged
merged 4 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wicked-cycles-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/theme': patch
---

Fix serving local assets from the root path for backward compatibility.
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ function findLocalFile(event: H3Event<EventHandlerRequest>, ctx: DevServerContex

// Try to match theme asset files first and fallback to theme extension asset files
return (
tryGetFile(/^\/cdn\/.*?\/assets\/([^?]+)/, ctx.localThemeFileSystem) ??
tryGetFile(/^\/ext\/cdn\/extensions\/.*?\/assets\/([^?]+)/, ctx.localThemeExtensionFileSystem) ?? {
tryGetFile(/^(?:\/cdn\/.*?)?\/assets\/([^?]+)/, ctx.localThemeFileSystem) ??
tryGetFile(/^(?:\/ext\/cdn\/extensions\/.*?)?\/assets\/([^?]+)/, ctx.localThemeExtensionFileSystem) ?? {
isUnsynced: false,
karreiro marked this conversation as resolved.
Show resolved Hide resolved
fileKey: undefined,
file: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ describe('setupDevServer', () => {
)
})

test('serves local assets from the root in a backward compatible way', async () => {
// Also serves assets from the root, similar to what the old server did:
const eventPromise = dispatchEvent('/assets/file2.css')
await expect(eventPromise).resolves.not.toThrow()

expect(vi.mocked(render)).not.toHaveBeenCalled()

const {res, body} = await eventPromise
expect(res.getHeader('content-type')).toEqual('text/css')
expect(body.toString()).toMatchInlineSnapshot(`".another-class {}"`)
})

test('gets the right content for assets with non-breaking spaces', async () => {
const eventPromise = dispatchEvent('/cdn/somepathhere/assets/file-with-nbsp.js')
await expect(eventPromise).resolves.not.toThrow()
Expand Down