Implement a Sitecore form in a JSS Next.js app
After you have created a Sitecore form and added the form to a JSS app route in Sitecore, you can implement a JSS Form component that renders the Sitecore Form in your JSS application.
Using Sitecore Forms in JSS apps requires functioning session cookies. For this purpose:
-
You must have SSL enabled for your environment or disabled secure cookies in your Sitecore instance.
-
Your Next.js JSS app uses the REST Layout Service.
-
You are server-side rendering the route displaying the form using an implementation of the function
GetServerSideProps.
To render a Sitecore Form component in a JSS Next.js application:
-
In the root directory of your JSS application, install the necessary packages by running the following command:
-
In the
srcdirectory of the rendering app, in a component file, for example,src/components/JssNextForm.tsx, implement your form component:NoteYou must set
sitecoreApiHostto an empty string, for example,sitecoreApiHost={''}, to send requests to the Next.js server. Otherwise, the rewrite you set up in the following step does not work as expected. -
In the
next.config.base.jsfile, add the following rewrite definition to the connected mode rewrites to prevent CORS and 404 errors: -
Start the JSS app in connected mode with
jss start:connected.