Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sitecore-jss-nextjs] Link component prefetches files #1956

Merged
merged 3 commits into from
Oct 23, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Our versioning strategy is as follows:

### 🐛 Bug Fixes

* `[sitecore-jss-nextjs]` Link component prefetches files ([#1956](https://github.com/Sitecore/jss/pull/1956))
* `[templates/nextjs]` `[templates/react]` `[templates/angular]` `[templates/vue]` Fixed an issue when environment variable is undefined (not present in .env), that produced an "undefined" value in generated config file ([#1875](https://github.com/Sitecore/jss/pull/1875))
* `[templates/nextjs]` Fix embedded personalization not rendering correctly after navigation through router links. ([#1911](https://github.com/Sitecore/jss/pull/1911))
* `[template/angular]` Prevent client-side dictionary API call when SSR data is available ([#1930](https://github.com/Sitecore/jss/pull/1930)) ([#1932](https://github.com/Sitecore/jss/pull/1932))
Expand Down
44 changes: 44 additions & 0 deletions packages/sitecore-jss-nextjs/src/components/Link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,50 @@ describe('<Link />', () => {
expect(rendered.find(ReactLink).length).to.equal(0);
});

describe('relative file url', () => {
it('should not render Next link when file url is provided', () => {
const field = {
value: {
href: '/foo/bar/test.html',
text: 'ipsum',
class: 'my-link',
title: 'My Link',
target: '_blank',
},
};
const rendered = mount(
<Page>
<Link field={field} showLinkTextWithChildrenPresent>
<p>Hello world...</p>
</Link>
</Page>
);
expect(rendered.find(NextLink).length).to.equal(0);
expect(rendered.find(ReactLink).length).to.equal(1);
});

it('should not render Next link when file url is provided in the root', () => {
const field = {
value: {
href: '/test.png',
text: 'ipsum',
class: 'my-link',
title: 'My Link',
target: '_blank',
},
};
const rendered = mount(
<Page>
<Link field={field} showLinkTextWithChildrenPresent>
<p>Hello world...</p>
</Link>
</Page>
);
expect(rendered.find(NextLink).length).to.equal(0);
expect(rendered.find(ReactLink).length).to.equal(1);
});
});

it('should render ReactLink if link is external', () => {
const field = {
value: {
Expand Down
12 changes: 10 additions & 2 deletions packages/sitecore-jss-nextjs/src/components/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export type LinkProps = ReactLinkProps & {
internalLinkMatcher?: RegExp;
};

/**
* Matches relative URLs that end with a file extension.
*/
const FILE_EXTENSION_MATCHER = /^\/.*\.\w+$/;

export const Link = forwardRef<HTMLAnchorElement, LinkProps>(
(props: LinkProps, ref): JSX.Element | null => {
const {
Expand Down Expand Up @@ -50,8 +55,11 @@ export const Link = forwardRef<HTMLAnchorElement, LinkProps>(
if (href && !isEditing) {
const text = showLinkTextWithChildrenPresent || !children ? value.text || value.href : null;

// determine if a link is a route or not.
if (internalLinkMatcher.test(href)) {
const isMatching = internalLinkMatcher.test(href);
const isFileUrl = FILE_EXTENSION_MATCHER.test(href);

// determine if a link is a route or not. File extensions are not routes and should not be pre-fetched.
if (isMatching && !isFileUrl) {
return (
<NextLink
href={{ pathname: href, query: querystring, hash: anchor }}
Expand Down