Skip to content

Commit

Permalink
Merge pull request #4624 from Shopify/fd-fix-local-asset-serve
Browse files Browse the repository at this point in the history
[Theme] Fix local asset serve for backward compatibility
  • Loading branch information
frandiox authored Oct 17, 2024
2 parents 15e772f + bd948c5 commit fa068c1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
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,
fileKey: undefined,
file: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,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

0 comments on commit fa068c1

Please sign in to comment.