Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/routing/Link.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 };
Expand Down
17 changes: 17 additions & 0 deletions tests/routing/unit/Link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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' }), {
Expand Down