Skip to content

Commit 3ac7982

Browse files
committed
Open specific template URL in theme editor shortcut
1 parent b34576d commit 3ac7982

File tree

8 files changed

+49
-1
lines changed

8 files changed

+49
-1
lines changed

.changeset/metal-lemons-reflect.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@shopify/theme': patch
3+
---
4+
5+
Editor shortcut (e) will now navigate to your most recently viewed template
6+
7+
Hitting `e` while your server is running (`shopify theme dev`) will now open
8+
the theme editor in the Admin to the most recently rendered template that you
9+
viewed in the browser (e.g. if you have multiple tabs open it will use the
10+
one that was rendered most recently).

packages/theme/src/cli/services/dev.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export async function dev(options: DevOptions) {
8383
localThemeExtensionFileSystem,
8484
directory: options.directory,
8585
type: 'theme',
86+
lastRequestedPath: '',
8687
options: {
8788
themeEditorSync: options['theme-editor-sync'],
8889
host,
@@ -132,7 +133,12 @@ export async function dev(options: DevOptions) {
132133
openURLSafely(urls.preview, 'theme preview')
133134
break
134135
case 'e':
135-
openURLSafely(urls.themeEditor, 'theme editor')
136+
openURLSafely(
137+
ctx.lastRequestedPath === '/'
138+
? urls.themeEditor
139+
: `${urls.themeEditor}&previewPath=${encodeURIComponent(ctx.lastRequestedPath)}`,
140+
'theme editor',
141+
)
136142
break
137143
case 'g':
138144
openURLSafely(urls.giftCard, 'gift card preview')

packages/theme/src/cli/utilities/theme-environment/hot-reload/server.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ function createTestContext(options?: {files?: [string, string][]}) {
542542
storeFqdn: 'my-store.myshopify.com',
543543
sessionCookies: {},
544544
},
545+
lastRequestedPath: '',
545546
localThemeFileSystem,
546547
localThemeExtensionFileSystem,
547548
directory: 'tmp',

packages/theme/src/cli/utilities/theme-environment/html.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,30 @@ describe('getHtmlHandler', async () => {
3535
options: {},
3636
localThemeExtensionFileSystem: emptyThemeExtFileSystem(),
3737
localThemeFileSystem: emptyThemeFileSystem(),
38+
lastRequestedPath: '',
3839
} as unknown as DevServerContext
3940

41+
test('sets lastRequestedPath to the rendering URL', async () => {
42+
const handler = getHtmlHandler(theme, ctx)
43+
44+
expect(ctx.lastRequestedPath).toStrictEqual('')
45+
46+
const event = createH3Event('GET', '/search?q=foo&options%5Bprefix%5D=last')
47+
48+
vi.mocked(render).mockResolvedValueOnce(
49+
new Response('', {
50+
status: 200,
51+
headers: {
52+
'x-request-id': 'test-request-id',
53+
},
54+
}),
55+
)
56+
57+
await handler(event)
58+
59+
expect(ctx.lastRequestedPath).toStrictEqual('/search?q=foo&options%5Bprefix%5D=last')
60+
})
61+
4062
test('the development server session recovers when a theme id mismatch occurs', async () => {
4163
// Given
4264
const handler = getHtmlHandler(theme, ctx)

packages/theme/src/cli/utilities/theme-environment/html.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export function getHtmlHandler(theme: Theme, ctx: DevServerContext): EventHandle
2323
return defineEventHandler((event) => {
2424
const [browserPathname = '/', browserSearch = ''] = event.path.split('?')
2525

26+
ctx.lastRequestedPath = event.path
27+
2628
const shouldRenderUploadErrorPage =
2729
ctx.options.errorOverlay !== 'silent' && ctx.localThemeFileSystem.uploadErrors.size > 0
2830

packages/theme/src/cli/utilities/theme-environment/theme-environment.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ describe('setupDevServer', () => {
8686
storeFqdn: 'my-store.myshopify.com',
8787
sessionCookies: {},
8888
},
89+
lastRequestedPath: '',
8990
localThemeFileSystem,
9091
localThemeExtensionFileSystem,
9192
directory: 'tmp',

packages/theme/src/cli/utilities/theme-environment/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ export interface DevServerContext {
8585
*/
8686
type: 'theme' | 'theme-extension'
8787

88+
/**
89+
* Tracks the last requested HTML path for keyboard shortcuts.
90+
*/
91+
lastRequestedPath: string
92+
8893
/**
8994
* Additional options for the development server.
9095
*/

packages/theme/src/cli/utilities/theme-ext-environment/theme-ext-server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ async function contextDevServerContext(
8181
localThemeExtensionFileSystem,
8282
directory,
8383
type: 'theme-extension',
84+
lastRequestedPath: '',
8485
options: {
8586
themeEditorSync: false,
8687
noDelete: false,

0 commit comments

Comments
 (0)