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

Shortcuts don't use the query params #328 #586

Merged
merged 2 commits into from
May 13, 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
14 changes: 10 additions & 4 deletions src/guillotine/validateShortcut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ import {UrlProcessor} from '../common/UrlProcessor';

export function validateShortcut(props: FetchContentResult): void {
const {data, meta, error} = props;
const pageUrl = data?.get?.data?.target?.pageUrl;
const dataObj = data?.get?.data;
const pageUrl = dataObj?.target?.pageUrl;
const parameters = dataObj?.parameters;
if (meta.type === 'base:shortcut' && pageUrl) {
if (meta.renderMode !== RENDER_MODE.NEXT) {
// do not show shortcut targets in preview/edit mode
console.warn(404, `Shortcuts are not available in ${RENDER_MODE.NEXT} render mode`);
console.warn(404, `Shortcuts are not available in ${meta.renderMode} render mode`);
notFound();
}

const destination = UrlProcessor.process(pageUrl, meta, true);
let destination = UrlProcessor.process(pageUrl, meta, true);
if (parameters) {
const searchParams = parameters.map(({name, value}) => `${encodeURIComponent(name)}=${encodeURIComponent(value)}`).join('&');
destination += '?' + searchParams;
}
console.debug(`Redirecting shortcut content [${meta.path}] to`, destination);
redirect(destination, RedirectType.replace);
}
Expand All @@ -34,4 +40,4 @@ export function validateShortcut(props: FetchContentResult): void {
console.warn(404, `Can not render content at [${meta.path}]`);
notFound();
}
}
}
4 changes: 4 additions & 0 deletions src/query/Shortcut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export const getShortcutQuery = `
target {
pageUrl(type: absolute)
}
parameters {
name
value
}
}
}
}
Expand Down
77 changes: 60 additions & 17 deletions test/guillotine/validateData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {FetchContentResult} from '../../src';
import {afterEach, beforeEach, describe, expect, jest, test as it} from '@jest/globals';
import {setupServerEnv} from '../constants';
import {RENDER_MODE, XP_COMPONENT_TYPE, XP_REQUEST_TYPE} from '../../src/common/constants';

import {RedirectType} from 'next/navigation';

globalThis.console = {
// error: console.error,
Expand All @@ -16,20 +16,9 @@ globalThis.console = {
} as unknown as Console;


const OLD_ENV = process.env;

const notFound = jest.fn();
const redirect = jest.fn();

const data = {
get: {
data: {
target: {
pageUrl: '/site/playground/2-column-test'
}
}
}
};

describe('guillotine', () => {

Expand Down Expand Up @@ -85,8 +74,8 @@ describe('guillotine', () => {
common: null,
data: null,
error: {
code: '404',
message: 'Not Found'
code: '404',
message: 'Not Found'
},
meta: {
type: '',
Expand Down Expand Up @@ -115,8 +104,8 @@ describe('guillotine', () => {
common: null,
data: null,
error: {
code: 'NOT404',
message: 'Error message'
code: 'NOT404',
message: 'Error message'
},
meta: {
type: '',
Expand All @@ -139,7 +128,7 @@ describe('guillotine', () => {
});
});

it('calls next/navigation.notFound and redirect when meta.renderMode not next', () => {
it('calls next/navigation.notFound and redirect when meta.type is base:shortcut and meta.renderMode is not next', () => {
const pageComponent = {
type: XP_COMPONENT_TYPE.PAGE,
path: '/site/playground/2-column-test'
Expand Down Expand Up @@ -179,6 +168,60 @@ describe('guillotine', () => {
});
});

it('calls redirect when meta.type is base:shortcut and meta.renderMode is next', () => {
const pageComponent = {
type: XP_COMPONENT_TYPE.PAGE,
path: '/site/playground/2-column-test'
};
const fetchContentResult = {
data: {
get: {
data: {
target: {
pageUrl: '/redirect/page'
},
parameters: [
{
name: 'intValue',
value: 1,
},
{
name: 'boolValue',
value: true,
},
{
name: 'strValue',
value: 'string',
}
]
}
}
},
common: {},
meta: {
type: 'base:shortcut',
path: '/site/playground/2-column-test',
requestType: XP_REQUEST_TYPE.PAGE,
renderMode: RENDER_MODE.NEXT,
requestedComponent: pageComponent,
canRender: true,
catchAll: false,
apiUrl: 'http://localhost:8080/site/_/service/com.enonic.app.enonic/guillotine/query',
baseUrl: 'http://localhost:8080/site',
locale: 'no',
defaultLocale: 'en',
},
page: pageComponent
};
import('../../src').then((moduleName) => {
expect(notFound).not.toHaveBeenCalled();
expect(redirect).not.toHaveBeenCalled();
moduleName.validateData(fetchContentResult);
expect(notFound).not.toHaveBeenCalled();
expect(redirect).toHaveBeenCalledWith("/no/redirect/page?intValue=1&boolValue=true&strValue=string", RedirectType.replace);
});
});

it('calls next/navigation.notFound when meta.canRender is false and meta.type is not base:shortcut', () => {
const pageComponent = {
type: XP_COMPONENT_TYPE.PAGE,
Expand Down