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

[NextJs] Sample app configuration options for 'jss create' #618

Merged
merged 5 commits into from
Mar 9, 2021
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
89 changes: 89 additions & 0 deletions samples/nextjs/jss-create.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const { applyNameToProject } = require('@sitecore-jss/sitecore-jss-cli/dist/create');

/**
Expand All @@ -17,5 +20,91 @@ module.exports = function createJssProject(argv, nextSteps) {

applyNameToProject(__dirname, argv.name, argv.hostName, 'JssNextWeb');

if (!argv.fetchWith || !argv.prerender) {
nextSteps.push([
`* Did you know you can customize the Next.js sample app using ${chalk.green('jss create')} parameters?`,
`* ${chalk.green('--fetchWith {REST|GraphQL}')} : Specifies how Sitecore data (layout, dictionary) is fetched. Default is REST.`,
`* ${chalk.green('--prerender {SSG|SSR}')} : Specifies the Next.js pre-rendering form for the optional catch-all route. Default is SSG.`,
]);
}

setFetchWith(argv.fetchWith);
setPrerender(argv.prerender);

return nextSteps;
};

/**
* Sets how Sitecore data (layout, dictionary) is fetched.
* @param {string} [fetchWith] {REST|GraphQL} Default is REST.
*/
function setFetchWith(fetchWith) {
const defaultDsfFile = path.join(__dirname, 'src/lib/dictionary-service-factory.ts');
const graphqlDsfFile = path.join(__dirname, 'src/lib/dictionary-service-factory.graphql.ts');
const defaultLsfFile = path.join(__dirname, 'src/lib/layout-service-factory.ts');
const graphqlLsfFile = path.join(__dirname, 'src/lib/layout-service-factory.graphql.ts');
const FetchWith = {
ambrauer marked this conversation as resolved.
Show resolved Hide resolved
REST: 'rest',
GRAPHQL: 'graphql',
};
let value = fetchWith ? fetchWith.toLowerCase() : FetchWith.REST;

if (value !== FetchWith.REST && value !== FetchWith.GRAPHQL) {
ambrauer marked this conversation as resolved.
Show resolved Hide resolved
console.warn(chalk.yellow(`Unsupported fetchWith value '${fetchWith}'. Using default 'REST'.`));
value = FetchWith.REST;
}

console.log(
chalk.cyan(`Applying ${value === FetchWith.REST ? 'REST' : 'GraphQL'} fetch...`)
);

switch (value) {
case FetchWith.REST:
fs.unlinkSync(graphqlDsfFile);
fs.unlinkSync(graphqlLsfFile);
break;

case FetchWith.GRAPHQL:
fs.unlinkSync(defaultDsfFile);
fs.renameSync(graphqlDsfFile, defaultDsfFile);
fs.unlinkSync(defaultLsfFile);
fs.renameSync(graphqlLsfFile, defaultLsfFile);
break;
}
}

/**
* Sets the Next.js pre-rendering form for the optional catch-all route.
* @param {string} [prerender] {SSG|SSR} Default is SSG.
*/
function setPrerender(prerender) {
const defaultRouteFile = path.join(__dirname, 'src/pages/[[...path]].tsx');
const ssrRouteFile = path.join(__dirname, 'src/pages/[[...path]].SSR.tsx');
const sitemapFile = path.join(__dirname, 'src/lib/sitemap-fetcher.ts');
const Prerender = {
SSG: 'ssg',
SSR: 'ssr',
};
let value = prerender ? prerender.toLowerCase() : Prerender.SSG;

if (value !== Prerender.SSG && value !== Prerender.SSR) {
console.warn(chalk.yellow(`Unsupported prerender value '${prerender}'. Using default 'SSG'.`));
value = Prerender.SSG;
}

console.log(
chalk.cyan(`Applying ${value.toUpperCase()} prerender...`)
);

switch (value) {
case Prerender.SSG:
fs.unlinkSync(ssrRouteFile);
break;

case Prerender.SSR:
fs.unlinkSync(defaultRouteFile);
fs.renameSync(ssrRouteFile, defaultRouteFile);
fs.unlinkSync(sitemapFile);
break;
}
}
9 changes: 9 additions & 0 deletions samples/nextjs/src/lib/dictionary-service-factory.graphql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { DictionaryService } from '@sitecore-jss/sitecore-jss-nextjs';

export class DictionaryServiceFactory {
create(): DictionaryService {
throw new Error('GraphQLDictionaryService not implemented!');
}
}

export const dictionaryServiceFactory = new DictionaryServiceFactory();
14 changes: 14 additions & 0 deletions samples/nextjs/src/lib/dictionary-service-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { DictionaryService, RestDictionaryService } from '@sitecore-jss/sitecore-jss-nextjs';
import config from 'temp/config';

export class DictionaryServiceFactory {
create(): DictionaryService {
return new RestDictionaryService({
apiHost: config.sitecoreApiHost,
apiKey: config.sitecoreApiKey,
siteName: config.jssAppName,
});
}
}

export const dictionaryServiceFactory = new DictionaryServiceFactory();
9 changes: 9 additions & 0 deletions samples/nextjs/src/lib/layout-service-factory.graphql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { LayoutService } from '@sitecore-jss/sitecore-jss-nextjs';

export class LayoutServiceFactory {
create(): LayoutService {
throw new Error('GraphQLLayoutService not implemented!');
}
}

export const layoutServiceFactory = new LayoutServiceFactory();
14 changes: 14 additions & 0 deletions samples/nextjs/src/lib/layout-service-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { LayoutService, RestLayoutService } from '@sitecore-jss/sitecore-jss-nextjs';
import config from 'temp/config';

export class LayoutServiceFactory {
create(): LayoutService {
return new RestLayoutService({
apiHost: config.sitecoreApiHost,
apiKey: config.sitecoreApiKey,
siteName: config.jssAppName,
});
}
}

export const layoutServiceFactory = new LayoutServiceFactory();
20 changes: 4 additions & 16 deletions samples/nextjs/src/lib/page-props-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import {
ComponentPropsService,
DictionaryPhrases,
DictionaryService,
RestDictionaryService,
LayoutServiceData,
LayoutService,
RestLayoutService,
editingDataService,
} from '@sitecore-jss/sitecore-jss-nextjs';
import { SitecorePageProps } from 'lib/page-props';
import { dictionaryServiceFactory } from 'lib/dictionary-service-factory';
import { layoutServiceFactory } from 'lib/layout-service-factory';
import { componentModule } from 'temp/componentFactory';
import { config as packageConfig } from '../../package.json';
import config from 'temp/config';

/**
* Extract normalized Sitecore item path from query
Expand Down Expand Up @@ -50,19 +49,8 @@ export class SitecorePagePropsFactory {

constructor() {
this.componentPropsService = new ComponentPropsService();

// Note we're using our standard REST-based dictionary and layout services here,
// but in the very near future we'll also have GraphQL-based counterparts available (for Sitecore Experience Edge).
this.dictionaryService = new RestDictionaryService({
apiHost: config.sitecoreApiHost,
apiKey: config.sitecoreApiKey,
siteName: config.jssAppName,
});
this.layoutService = new RestLayoutService({
apiHost: config.sitecoreApiHost,
apiKey: config.sitecoreApiKey,
siteName: config.jssAppName,
});
this.dictionaryService = dictionaryServiceFactory.create();
this.layoutService = layoutServiceFactory.create();
}

/**
Expand Down