From cae97da71b819b30a604db5d370f769a9308642e Mon Sep 17 00:00:00 2001 From: Anthony Gubler Date: Thu, 16 Apr 2020 12:37:39 +0100 Subject: [PATCH] Let the browser deal with url changes when in btr and using the cli serve --- src/routing/Link.ts | 7 +++++-- tests/routing/unit/Link.ts | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/routing/Link.ts b/src/routing/Link.ts index 442df543f..eec32efef 100644 --- a/src/routing/Link.ts +++ b/src/routing/Link.ts @@ -1,4 +1,5 @@ import { create, v } from '../core/vdom'; +import has from '../core/has'; import injector from '../core/middleware/injector'; import { VNodeProperties } from '../core/interfaces'; import { Params } from './interfaces'; @@ -29,8 +30,10 @@ export const Link = factory(function Link({ middleware: { injector }, properties onClick && onClick(event); if (!event.defaultPrevented && event.button === 0 && !event.metaKey && !event.ctrlKey && !target) { - event.preventDefault(); - href !== undefined && router.setPath(href); + if (!has('build-serve') || !has('build-time-rendered')) { + event.preventDefault(); + href !== undefined && router.setPath(href); + } } }; linkProps = { ...props, onclick, href }; diff --git a/tests/routing/unit/Link.ts b/tests/routing/unit/Link.ts index a7bab6e30..9b39aac51 100644 --- a/tests/routing/unit/Link.ts +++ b/tests/routing/unit/Link.ts @@ -2,6 +2,7 @@ const { beforeEach, afterEach, describe, it } = intern.getInterface('bdd'); const { assert } = intern.getPlugin('chai'); import { spy, SinonSpy } from 'sinon'; +import { add } from '../../../src/core/has'; import { v, w, create, getRegistry } from '../../../src/core/vdom'; import { Registry } from '../../../src/core/Registry'; import { Link } from '../../../src/routing/Link'; @@ -68,6 +69,8 @@ describe('Link', () => { afterEach(() => { routerSetPathSpy.restore(); + add('build-serve', false, true); + add('build-time-rendered', false, true); }); it('Generate link component for basic outlet', () => { @@ -163,6 +166,20 @@ describe('Link', () => { assert.isTrue(routerSetPathSpy.notCalled); }); + it('does not call router when build serve and build time rendered is detected', () => { + add('build-serve', true, true); + add('build-time-rendered', true, true); + const WrappedAnchor = wrap('a'); + const r = renderer(() => w(Link, { to: '#foo/static', isOutlet: false }), { + middleware: [[getRegistry, mockGetRegistry]] + }); + const template = assertion(() => v(WrappedAnchor.tag, { href: '#foo/static', onclick: noop })); + r.expect(template); + r.property(WrappedAnchor, 'onclick', createMockEvent()); + r.expect(template); + assert.isTrue(routerSetPathSpy.notCalled); + }); + it('throw error if the injected router cannot be found with the router key', () => { try { renderer(() => w(Link, { to: '#foo/static', isOutlet: false, routerKey: 'fake-key' }), {