diff --git a/docs/upgrades/unreleased.md b/docs/upgrades/unreleased.md index 4121642a3a..e7783563d6 100644 --- a/docs/upgrades/unreleased.md +++ b/docs/upgrades/unreleased.md @@ -69,6 +69,42 @@ Most of this re-iterates the instructions already provided in 21.6 and 21.7 upgr * Update RedirectsMiddleware at `/src/lib/middleware/plugins/redirects.ts` to use clientFactory if it wasn't as part of ([JSS 21.6 upgrade guide](https://doc.sitecore.com/xmc/en/developers/jss/217/jss-xmc/upgrade-jss-21-5-next-js-apps-to-version-21-6.html)). +* If you have not customized the `\src\pages\api\sitemap.ts` file, replace it with the 22.0 version. Otherwise, do the following: + + Add the following import statement: + ``` + import clientFactory from 'lib/graphql-client-factory'; + ``` + + Replace the endpoint and apiKey options for the GraphQLSitemapXmlService instantiation with the client factory: + + ``` + const sitemapXmlService = new GraphQLSitemapXmlService({ + clientFactory, + siteName: site.name, + }); + ``` + +* If you have not customized the `\src\pages\api\robots.ts` file, replace it with the 22.0 version. Otherwise, do the following: + + Add the following import statement: + ``` + import clientFactory from 'lib/graphql-client-factory'; + ``` + + Remove the `config` import: + ``` + import config from 'temp/config'; + ``` + + Replace the endpoint and apiKey options for the GraphQLRobotsService instantiation with the client factory: + + ``` + const sitemapXmlService = new GraphQLRobotsService({ + clientFactory, + siteName: site.name, + }); + ``` ### nextjs-multisite diff --git a/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/pages/api/robots.ts b/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/pages/api/robots.ts index ce820587be..3032866757 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/pages/api/robots.ts +++ b/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/pages/api/robots.ts @@ -1,7 +1,7 @@ import type { NextApiRequest, NextApiResponse } from 'next'; import { GraphQLRobotsService } from '@sitecore-jss/sitecore-jss-nextjs'; import { siteResolver } from 'lib/site-resolver'; -import config from 'temp/config'; +import clientFactory from 'lib/graphql-client-factory'; const robotsApi = async (req: NextApiRequest, res: NextApiResponse): Promise => { res.setHeader('Content-Type', 'text/plain'); @@ -12,8 +12,7 @@ const robotsApi = async (req: NextApiRequest, res: NextApiResponse): Promise { - // because this is a proxy, all headers are forwarded on to the Sitecore server - // but, if we SSR we only understand how to decompress gzip and deflate. Some - // modern browsers would send 'br' (brotli) as well, and if the Sitecore server - // supported that (maybe via CDN) it would fail SSR as we can't decode the Brotli - // response. So, we force the accept-encoding header to only include what we can understand. - if (req.headers['accept-encoding']) { - req.headers['accept-encoding'] = 'gzip, deflate'; - } - - next(); -}); - -// For any other requests, we render app routes server-side and return them -server.use('*', scProxy(config.serverBundle.renderView, config, config.serverBundle.parseRouteUrl)); - -server.listen(port, () => { - console.log(`server listening on port ${port}!`); -}); +import express from 'express'; +import compression from 'compression'; +import 'dotenv/config'; +import scProxy from '@sitecore-jss/sitecore-jss-proxy'; +import { config } from './config'; +//import { cacheMiddleware } from './cacheMiddleware'; + +const server = express(); +const port = process.env.PORT || 3000; + +// enable gzip compression for appropriate file types +server.use(compression()); + +// turn off x-powered-by http header +server.settings['x-powered-by'] = false; + +// Serve static app assets from local /dist folder +server.use( + '/dist', + express.static('dist', { + fallthrough: false, // force 404 for unknown assets under /dist + }) +); + +/** + * Output caching, can be enabled, + * Read about restrictions here: {@link https://doc.sitecore.com/xp/en/developers/hd/22/sitecore-headless-development/caching-in-headless-server-side-rendering-mode.html} + */ +//server.use(cacheMiddleware()); + +server.use((req, _res, next) => { + // because this is a proxy, all headers are forwarded on to the Sitecore server + // but, if we SSR we only understand how to decompress gzip and deflate. Some + // modern browsers would send 'br' (brotli) as well, and if the Sitecore server + // supported that (maybe via CDN) it would fail SSR as we can't decode the Brotli + // response. So, we force the accept-encoding header to only include what we can understand. + if (req.headers['accept-encoding']) { + req.headers['accept-encoding'] = 'gzip, deflate'; + } + + next(); +}); + +// For any other requests, we render app routes server-side and return them +server.use('*', scProxy(config.serverBundle.renderView, config, config.serverBundle.parseRouteUrl)); + +server.listen(port, () => { + console.log(`server listening on port ${port}!`); +});