diff --git a/.changeset/mighty-gorillas-fail.md b/.changeset/mighty-gorillas-fail.md new file mode 100644 index 000000000..cb3128cfd --- /dev/null +++ b/.changeset/mighty-gorillas-fail.md @@ -0,0 +1,6 @@ +--- +'@verdaccio/middleware': patch +'@verdaccio/url': patch +--- + +fix(middleware): link to favicon in template diff --git a/packages/core/url/tests/getPublicUrl.spec.ts b/packages/core/url/tests/getPublicUrl.spec.ts index fb7984b07..2313f2fde 100644 --- a/packages/core/url/tests/getPublicUrl.spec.ts +++ b/packages/core/url/tests/getPublicUrl.spec.ts @@ -102,7 +102,7 @@ describe('host', () => { }); expect( - getPublicUrl(null, { + getPublicUrl(undefined, { host: req.hostname, headers: req.headers as any, protocol: req.protocol, diff --git a/packages/middleware/package.json b/packages/middleware/package.json index 56284674b..1a720efa8 100644 --- a/packages/middleware/package.json +++ b/packages/middleware/package.json @@ -35,6 +35,7 @@ "build:js": "babel src/ --out-dir build/ --copy-files --extensions \".ts,.tsx\" --source-maps", "watch": "pnpm build:js -- --watch", "test": "jest", + "test:snap": "jest --updateSnapshot", "build": "pnpm run build:js && pnpm run build:types" }, "dependencies": { diff --git a/packages/middleware/src/middlewares/web/utils/manifest.ts b/packages/middleware/src/middlewares/web/utils/manifest.ts index e63dd6750..4fa90a45e 100644 --- a/packages/middleware/src/middlewares/web/utils/manifest.ts +++ b/packages/middleware/src/middlewares/web/utils/manifest.ts @@ -16,8 +16,16 @@ export function getManifestValue( ): string[] { return manifestItems?.map((item) => { debug('resolve item %o', item); - const resolvedItem = `${basePath}${manifest[item]}`; + const resolvedItem = `${stripTrailingSlash(basePath)}/${stripLeadingSlash(manifest[item])}`; debug('resolved item %o', resolvedItem); return resolvedItem; }); } + +function stripTrailingSlash(path: string): string { + return path.replace(/\/$/, ''); +} + +function stripLeadingSlash(path: string): string { + return path.replace(/^\//, ''); +} diff --git a/packages/middleware/src/middlewares/web/utils/template.ts b/packages/middleware/src/middlewares/web/utils/template.ts index 8bb40a37e..1c528e159 100644 --- a/packages/middleware/src/middlewares/web/utils/template.ts +++ b/packages/middleware/src/middlewares/web/utils/template.ts @@ -25,24 +25,24 @@ export default function renderTemplate(template: Template, manifest: WebpackMani return ` - + - ${template?.options?.title ?? ''} - - + ${template?.options?.title ?? ''} + + ${template?.metaScripts ? template.metaScripts.join('') : ''} - + - ${template?.scriptsbodyBefore ? template.scriptsbodyBefore.join('') : ''} + ${template?.scriptsbodyBefore ? template.scriptsbodyBefore.join('') : ''}
${getManifestValue(template.manifest.js, manifest, template?.options.base) .map((item) => ``) - .join('')} + .join(`\n `)} ${template?.scriptsBodyAfter ? template.scriptsBodyAfter.join('') : ''} diff --git a/packages/middleware/test/__snapshots__/template.test.ts.snap b/packages/middleware/test/__snapshots__/template.test.ts.snap index d503fe251..1ec9c6016 100644 --- a/packages/middleware/test/__snapshots__/template.test.ts.snap +++ b/packages/middleware/test/__snapshots__/template.test.ts.snap @@ -3,22 +3,23 @@ exports[`template custom body after 1`] = ` " - + - - + + - + - + - +
- + + - + - + + @@ -53,22 +55,23 @@ exports[`template custom body before 1`] = ` exports[`template custom render 1`] = ` " - + - - + + - + - + - +
- + + @@ -78,47 +81,23 @@ exports[`template custom render 1`] = ` exports[`template custom title 1`] = ` " - + - - foo title + + foo title - + - + - -
- - - - " -`; - -exports[`template custom title 2`] = ` -" - - - - - - foo title - - - - - - -
- + + @@ -128,22 +107,23 @@ exports[`template custom title 2`] = ` exports[`template meta scripts 1`] = ` " - + - - + + - + - + - +
- + + diff --git a/packages/middleware/test/manifest.test.ts b/packages/middleware/test/manifest.test.ts index 97416a1b6..2f2eb5876 100644 --- a/packages/middleware/test/manifest.test.ts +++ b/packages/middleware/test/manifest.test.ts @@ -8,4 +8,16 @@ describe('manifest', () => { '/-/static/main.6126058572f989c948b1.js', ]); }); + + test('getManifestValue with base', () => { + expect(getManifestValue(['favicon.ico'], manifest, 'http://domain.com')).toEqual([ + 'http://domain.com/-/static/favicon.ico', + ]); + }); + + test('getManifestValue with base with trailing slash', () => { + expect(getManifestValue(['favicon.ico'], manifest, 'http://domain.com/')).toEqual([ + 'http://domain.com/-/static/favicon.ico', + ]); + }); }); diff --git a/packages/middleware/test/template.test.ts b/packages/middleware/test/template.test.ts index 489853571..14ad6026b 100644 --- a/packages/middleware/test/template.test.ts +++ b/packages/middleware/test/template.test.ts @@ -2,32 +2,27 @@ import template from '../src/middlewares/web/utils/template'; const manifest = require('./partials/manifest/manifest.json'); +// manifest expected to have leading slash +// see packages\middleware\test\partials\manifest\manifest.json const exampleManifest = { css: ['main.css'], js: ['runtime.js', 'main.js'], - ico: '/static/foo.ico', + ico: 'favicon.ico', }; +// "base" is expected to be result of getPublicUrl +// i.e. it must be valid URL with trailing slash describe('template', () => { test('custom render', () => { expect( - template({ options: { base: 'http://domain.com' }, manifest: exampleManifest }, manifest) + template({ options: { base: 'http://domain.com/' }, manifest: exampleManifest }, manifest) ).toMatchSnapshot(); }); test('custom title', () => { expect( template( - { options: { base: 'http://domain.com', title: 'foo title' }, manifest: exampleManifest }, - manifest - ) - ).toMatchSnapshot(); - }); - - test('custom title', () => { - expect( - template( - { options: { base: 'http://domain.com', title: 'foo title' }, manifest: exampleManifest }, + { options: { base: 'http://domain.com/', title: 'foo title' }, manifest: exampleManifest }, manifest ) ).toMatchSnapshot(); @@ -37,7 +32,7 @@ describe('template', () => { expect( template( { - options: { base: 'http://domain.com' }, + options: { base: 'http://domain.com/' }, metaScripts: [``], manifest: exampleManifest, }, @@ -50,7 +45,7 @@ describe('template', () => { expect( template( { - options: { base: 'http://domain.com' }, + options: { base: 'http://domain.com/' }, scriptsBodyAfter: [`