Skip to content

feat: add preview side editor #38

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

Merged
merged 14 commits into from
Apr 30, 2025
45 changes: 22 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![LaunchPad](./LaunchPad.jpg)

Welcome aboard **LaunchPad**, the official Strapi demo application, where we launch your content into the stratosphere at the speed of *"we-can't-even-measure-it!"*.
Welcome aboard **LaunchPad**, the official Strapi demo application, where we launch your content into the stratosphere at the speed of _"we-can't-even-measure-it!"_.
This repository contains the following:

- A Strapi project with content-types and data already onboard
Expand All @@ -12,22 +12,6 @@ This repository contains the following:

Strap yourself in! You can get started with this project on your local machine by following the instructions below, or you can [request a private instance on our website](https://strapi.io/demo)

## 🛠 Prerequisites

Before you take off, make sure you have the right env variables loaded for each part (rocket science at its best):

Strapi (example in `./strapi/.env.example`):
- `STRAPI_ADMIN_CLIENT_URL=<url-of-nextjs>`
- `STRAPI_ADMIN_CLIENT_PREVIEW_SECRET=<a-random-token>`

- Create a `./strapi/.env` file with these variables

Next.js (example in `./next/.env.sample`):
- `NEXT_PUBLIC_API_URL=<url-of-strapi>` (mandatory)
- `PREVIEW_SECRET=<the-same-random-token-as-for-strapi>`

- Create a `./next/.env` file with these variables

## 1. Clone Launchpad

To infinity and beyond! 🚀 Clone the repo with this command:
Expand All @@ -38,7 +22,23 @@ git clone https://github.com/strapi/launchpad.git

- Navigate to your project folder by running `cd launchpad`.

## 2. Start Strapi
## 2. Set up environment variables

Before you take off, set up the required environment variables for both Strapi and Next.js.

To create the Strapi .env file, copy the content of the `./strapi/.env.example` file into a new file named `./strapi/.env`, then modify the values to match your setup:

```sh
cp ./strapi/.env.example ./strapi/.env
```

Then do the same for the Next.js .env file, and modify it too:

```sh
cp ./next/.env.example ./next/.env
```

## 3. Start Strapi

Take a deep breath. It's time to power up the Strapi engines. Navigate to your ./my-projects/launchpad/strapi folder by running:

Expand All @@ -52,11 +52,11 @@ yarn && yarn seed && yarn develop

This will install dependencies, sprinkle in some data magic, and run the server. (You can run these commands separately, but why not be efficient?)

## 3. Start Next.js
## 4. Start Next.js

We're almost ready for lift-off! Next.js is your sleek, futuristic interface for getting all that glorious content out into the world. 🚀

Navigate to your `./my-projects/launchpad/next` folder by running `cd next` from your command line.
Open a new terminal tab or window to leave Strapi running, and navigate to your `./my-projects/launchpad/next` folder by running `cd next`.

- Run the following command in your `./launchpad/next` folder

Expand Down Expand Up @@ -91,10 +91,9 @@ This installs dependencies, builds your project, and starts your server. You’r
[Data transfer](https://strapi.io/blog/importing-exporting-and-transferring-data-with-the-strapi-cli) Streams your data from one Strapi instance to another Strapi instance.<br />
[Review Worfklows](https://docs.strapi.io/user-docs/settings/review-workflows) Create and manage any desired review stages for your content, enabling your team to collaborate in the content creation flow from draft to publication. <br />


## Resources

[Docs](https://docs.strapi.io) • [Demo](https://strapi.io/demo) • [Forum](https://forum.strapi.io/) • [Discord](https://discord.strapi.io) • [Youtube](https://www.youtube.com/c/Strapi/featured) • [Strapi Design System](https://design-system.strapi.io/) • [Marketplace](https://market.strapi.io/) • [Cloud Free Trial](https://cloud.strapi.io)
[Docs](https://docs.strapi.io) • [Demo](https://strapi.io/demo) • [Forum](https://forum.strapi.io/) • [Discord](https://discord.strapi.io) • [Youtube](https://www.youtube.com/c/Strapi/featured) • [Strapi Design System](https://design-system.strapi.io/) • [Marketplace](https://market.strapi.io/) • [Cloud Free Trial](https://cloud.strapi.io)

## Todo

Expand All @@ -109,4 +108,4 @@ This installs dependencies, builds your project, and starts your server. You’r

- The Strapi application contains a postinstall script that will regenerate an uuid for the project in order to get some anonymous usage information concerning this demo. You can disable it by removing the uuid inside the `./strapi/packages.json` file.

- The Strapi application contains a patch for the @strapi/admin package. It is only necessary for the hosted demos since we automatically create the Super Admin users for them when they request this demo on our website.
- The Strapi application contains a patch for the @strapi/admin package. It is only necessary for the hosted demos since we automatically create the Super Admin users for them when they request this demo on our website.
2 changes: 1 addition & 1 deletion next/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ WEBSITE_URL=http://localhost:3000 # Add the correct ENV var for this onto your h
PORT=3000

NEXT_PUBLIC_API_URL=http://localhost:1337
PREVIEW_SECRET=the-same-random-token-as-for-strapi
PREVIEW_SECRET=tobemodified
2 changes: 1 addition & 1 deletion next/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Welcome to our cutting-edge Next.js Multilingual Blog Template! This powerful an

1. Clone this repository
2. Install dependencies: `npm install`
3. Copy `.env.sample` to `.env.local` and update the variables
3. Copy `.env.example` to `.env.local` and update the variables
4. Run the development server: `npm run dev`
5. Open [http://localhost:3000](http://localhost:3000) in your browser

Expand Down
22 changes: 22 additions & 0 deletions next/app/[locale]/(marketing)/ClientSlugHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { useEffect } from "react";
import { useSlugContext } from "@/app/context/SlugContext";
import { useRouter } from "next/navigation";

export default function ClientSlugHandler({
localizedSlugs,
Expand All @@ -16,5 +17,26 @@ export default function ClientSlugHandler({
}
}, [localizedSlugs, dispatch]);

const router = useRouter();

useEffect(() => {
const handleMessage = async (message: MessageEvent<any>) => {
if (
message.origin === process.env.NEXT_PUBLIC_API_URL &&
message.data.type === "strapiUpdate"
) {
router.refresh();
}
};

// Add the event listener
window.addEventListener("message", handleMessage);

// Cleanup the event listener on unmount
return () => {
window.removeEventListener("message", handleMessage);
};
}, [router]);

return null; // This component only handles the state and doesn't render anything.
}
4 changes: 1 addition & 3 deletions strapi/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ ADMIN_JWT_SECRET=tobemodified
TRANSFER_TOKEN_SALT=tobemodified
JWT_SECRET=tobemodified

STRAPI_ADMIN_CLIENT_URL=http://localhost:3000
STRAPI_ADMIN_CLIENT_PREVIEW_SECRET=a-random-token

CLIENT_URL=http://localhost:3000
PREVIEW_SECRET=your-secret-key # optional, required with Next.js draft mode
PREVIEW_SECRET=tobemodified # optional, required with Next.js draft mode
28 changes: 14 additions & 14 deletions strapi/config/admin.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
export default ({ env }) => ({
auth: {
secret: env('ADMIN_JWT_SECRET'),
secret: env("ADMIN_JWT_SECRET"),
},
apiToken: {
salt: env('API_TOKEN_SALT'),
salt: env("API_TOKEN_SALT"),
},
transfer: {
token: {
salt: env('TRANSFER_TOKEN_SALT'),
salt: env("TRANSFER_TOKEN_SALT"),
},
},
flags: {
nps: env.bool('FLAG_NPS', true),
promoteEE: env.bool('FLAG_PROMOTE_EE', true),
nps: env.bool("FLAG_NPS", true),
promoteEE: env.bool("FLAG_PROMOTE_EE", true),
},
preview: {
enabled: true,
config: {
allowedOrigins: [env('STRAPI_ADMIN_CLIENT_URL')],
allowedOrigins: [env("CLIENT_URL")],
async handler(uid, { documentId, locale, status }) {
const document = await strapi.documents(uid).findOne({
documentId, populate: null,
documentId,
populate: null,
fields: ["slug"],
});
const { slug } = document;

const urlSearchParams = new URLSearchParams({
secret: env('STRAPI_ADMIN_CLIENT_PREVIEW_SECRET'),
secret: env("PREVIEW_SECRET"),
...(slug && { slug }),
locale,
uid,
status
status,
});

const previewURL = `${env('STRAPI_ADMIN_CLIENT_URL')}/api/preview?${urlSearchParams}`
const previewURL = `${env("CLIENT_URL")}/api/preview?${urlSearchParams}`;

return previewURL
return previewURL;
},
}

}
},
},
});
6 changes: 6 additions & 0 deletions strapi/config/features.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default ({ env }) => ({
future: {
unstablePreviewSideEditor: true,
unstableRelationsOnTheFly: true,
},
});
8 changes: 4 additions & 4 deletions strapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
"typescript": "^5"
},
"dependencies": {
"@strapi/plugin-cloud": "5.7.0",
"@strapi/plugin-cloud": "5.12.3",
"@strapi/plugin-seo": "^2.0.4",
"@strapi/plugin-users-permissions": "5.7.0",
"@strapi/strapi": "5.7.0",
"@strapi/plugin-users-permissions": "5.12.3",
"@strapi/strapi": "5.12.3",
"better-sqlite3": "11.7.0",
"patch-package": "^8.0.0",
"pluralize": "^8.0.0",
Expand All @@ -43,4 +43,4 @@
"npm": ">=6.0.0"
},
"license": "MIT"
}
}
44 changes: 0 additions & 44 deletions strapi/src/admin/extensions/components/PreviewButton/index.tsx

This file was deleted.

Loading