System requirements
- Node.js 18.17 or a later version.
- macOS, Windows (including WSL), and Linux are supported.
src
directory and have the following path aliases configured.
tsconfig.json
Install dependencies
Install the@makeswift/runtime
package. This package contains all of the necessary
code to integrate Makeswift into your Next.js app.
Add API key to environment variables
Requesting data through theMakeswift
client requires a site API key from Makeswift. In the Makeswift builder, go to Settings > Host and copy the API key for the site.
.env.local
file and paste the snippet below.
Add Makeswift runtime
Create the Makeswift runtime file insrc/makeswift
.
src/makeswift/runtime.ts
Add Makeswift client
Create the Makeswift client file insrc/makeswift
.
src/makeswift/client.ts
Add the Next.js plugin
Next.js plugins are configured in the project’s next.config.js file by wrappingnextConfig
. The Makeswift Next.js plugin whitelists Makeswift image domains and sets up rewrites to enable preview mode in the Makeswift builder.
Set up a custom Document
The Makeswift custom Document handles styles during server-side rendering and using Preview Mode when opening your pages in the Makeswift builder. Create the filesrc/pages/_document.ts
and export Document
from @makeswift/runtime/next/document
:
src/pages/_document.tsx
_document.ts
, you can extend the Document
from @makeswift/runtime/next/document
instead.
Example of extending an existing document
Example of extending an existing document
src/pages/_document.tsx
Register components with Makeswift
Create a file for registered components calledsrc/makeswift/components.tsx
. In this example, only one component is registered. However, as you register more components, we recommend creating separate files for each component and rolling up the imports in the src/makeswift/components.ts
file. Learn more about registering components.
src/makeswift/components.tsx
components.tsx
file must be imported in three different places:
- In the custom app
- In the optional catch-all route
- In the Makeswift API Handler
Provide the runtime to Custom App
If you don’t have a Custom App you’ll need to create one. Then wrap your Custom App with the MakeswiftReactRuntimeProvider
component.
src/pages/_app.tsx
Add a route for Makeswift pages
Create an optional catch-all route named[[...path]].tsx
. You will use this route to fetch page snapshots from the Makeswift
client and render them using the Page
component.
-
If you already have a root
index.tsx
file inside of thepages
directory and you’d like to manage that page in code (not in Makeswift), you will need to name the file[...path].tsx
instead of[[...path]].tsx
. Otherwise, you’ll need to delete it to let the root page be managed in Makeswift. For more information about the differences between catch-all and optional catch-all segments, refer to the Next.js Catch-all segments documentation. -
The filename defines the
path
param. For example, if the filename is[[...slug]].tsx
instead of[[...path]].tsx
, then the param name isslug
. Because this is an optional catch-all route, there are no params when visiting the index (i.e.,/
) path. Thepath
param defaults to an empty array. -
fallback: 'blocking'
is used here so that your Next.js app doesn’t need to be re-deployed whenever a new Makeswift page is created.
revalidate
field isn’t added to the returned value of getStaticProps
because
Makeswift pages are automatically revalidated using
on-demand revalidation
by leveraging the Makeswift API handler.
Add the Makeswift API handler
Similar to NextAuth.js, Makeswift uses an API handler to communicate with your Next.js app. Create the filesrc/pages/api/makeswift/[...makeswift].ts
.
It is important this file has that exact name and path. The extension can be
.js
or .ts
.src/pages/api/makeswift/[...makeswift].ts
Start the local dev server
Run the local development script. This will start the Next.js app athttp://localhost:3000
.
3000
is already in use, Next.js will try port 3001
, then 3002
, and so forth until it finds an
unused port.
Take note of this port for the next step.
Add your app’s URL to Makeswift
Finally, open the Makeswift builder, navigate to Settings > Host, and add your app’s URL. If you haven’t changed anything in the example and the server is running on port3000
, the app’s URL should be
http://localhost:3000
.
http://localhost:3000
. You can keep this site for local development.
Start building
Great job! You should be able to create a page in Makeswift and start dropping in registered components from the left toolbar.