diff --git a/code/core/src/manager-api/modules/url.ts b/code/core/src/manager-api/modules/url.ts index b7f37e179e59..e46494a34cf4 100644 --- a/code/core/src/manager-api/modules/url.ts +++ b/code/core/src/manager-api/modules/url.ts @@ -254,7 +254,8 @@ export const init: ModuleFn = (moduleArgs) => { base === 'origin' ? originAddress : base === 'network' ? networkAddress : pathname; const previewBase = refId ? refs[refId].url + '/iframe.html' - : global.PREVIEW_URL || `${managerBase.replace(/\/[^/]*$/, '/')}iframe.html`; + : global.PREVIEW_URL || + `${managerBase.replace(/\/[^/]*\.html$/, '').replace(/\/?$/, '/')}iframe.html`; const refParam = refId ? `&refId=${encodeURIComponent(refId)}` : ''; const { args = '', globals = '', ...otherParams } = queryParams; diff --git a/code/core/src/manager-api/tests/url.test.js b/code/core/src/manager-api/tests/url.test.js index aedbc591d13e..4b259bdf6599 100644 --- a/code/core/src/manager-api/tests/url.test.js +++ b/code/core/src/manager-api/tests/url.test.js @@ -486,4 +486,49 @@ describe('getStoryHrefs', () => { expect(managerHref).toEqual('/index.html?path=/story/test--story'); expect(previewHref).toEqual('/iframe.html?id=test--story&viewMode=story'); }); + + it('correctly links when hosted at a subpath without trailing slash', () => { + const { api, state } = initURL({ + store, + provider: { channel: new EventEmitter() }, + state: { location: { pathname: '/design-system', search: '' } }, + navigate: vi.fn(), + fullAPI: { getCurrentStoryData: () => ({ id: 'test--story' }) }, + }); + store.setState(state); + + const { managerHref, previewHref } = api.getStoryHrefs('test--story'); + expect(managerHref).toEqual('/design-system?path=/story/test--story'); + expect(previewHref).toEqual('/design-system/iframe.html?id=test--story&viewMode=story'); + }); + + it('correctly links when hosted at a subpath with trailing slash', () => { + const { api, state } = initURL({ + store, + provider: { channel: new EventEmitter() }, + state: { location: { pathname: '/design-system/', search: '' } }, + navigate: vi.fn(), + fullAPI: { getCurrentStoryData: () => ({ id: 'test--story' }) }, + }); + store.setState(state); + + const { managerHref, previewHref } = api.getStoryHrefs('test--story'); + expect(managerHref).toEqual('/design-system/?path=/story/test--story'); + expect(previewHref).toEqual('/design-system/iframe.html?id=test--story&viewMode=story'); + }); + + it('correctly links when hosted at a subpath with index.html', () => { + const { api, state } = initURL({ + store, + provider: { channel: new EventEmitter() }, + state: { location: { pathname: '/design-system/index.html', search: '' } }, + navigate: vi.fn(), + fullAPI: { getCurrentStoryData: () => ({ id: 'test--story' }) }, + }); + store.setState(state); + + const { managerHref, previewHref } = api.getStoryHrefs('test--story'); + expect(managerHref).toEqual('/design-system/index.html?path=/story/test--story'); + expect(previewHref).toEqual('/design-system/iframe.html?id=test--story&viewMode=story'); + }); });