Skip to content

Instantly share code, notes, and snippets.

Last active January 29, 2023 13:42
What would you like to do?
NextJS Sitecore JSS Layout Service Response Shortcut URL
// This code makes it easier to ping your layout service without having to remember the long URL that is accessible by default.
// Add this file to /pages/api/viewpage or wherever you want.
// From there, all you need to do is visit /api/viewpage to view the layout service response for your home page
// Subpage would be /api/viewpage/someSubPage
// Note that this will be PUBLICLY VIEWABLE, so make sure to lock it down somehow, possibly with this or WAF rules:
import type { NextApiRequest, NextApiResponse } from 'next';
const handler = async (req: NextApiRequest, res: NextApiResponse): Promise<void> => {
const apiHost = process.env.SITECORE_API_HOST
? process.env.SITECORE_API_HOST
: ``;
const siteName = process.env.NEXT_PUBLIC_JSS_SITE_NAME
: `main`;
const apiKey = process.env.SITECORE_API_KEY
? process.env.SITECORE_API_KEY
const { path } = req.query;
const itemPath = path ? path : '/';
if (typeof itemPath === 'string') {
queryStringPath = itemPath;
} else {
queryStringPath = itemPath.join('/');
const layoutServiceResponse = await fetch(
res.status(200).json(await layoutServiceResponse.json());
export default handler;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment