How to Create a Preview Button in Strapi v3 for Next.js

Every day is a learning day at Strapi :slight_smile:

I have found the solution to load .env variables for this preview link…

Please refer to the following discussions:

Essentially, we cannot load .env variables into the “frontend” of Strapi (the frontend includes plugins and extensions are plugin extensions).

Therefore we have to customise webpack.

Create a file ./admin/admin.config.js with the folllowing:

module.exports = {
    webpack: (config, webpack) => {
        const definePlugin = new webpack.DefinePlugin({
            CLIENT_URL: JSON.stringify(process.env.CLIENT_URL),
            CLIENT_PREVIEW_SECRET: JSON.stringify(
                process.env.CLIENT_PREVIEW_SECRET
            ),
        });
        config.plugins.push(definePlugin);
        return config;
    },
};

Then do following:

  1. delete the ./build and ./cache folders.
  2. yarn build --clean

Now you will be able to access CLIENT_URL and CLIENT_PREVIEW_SECRET in ./extensions/admin/src/InjectedComponents/ExternalLink/index.js