Code Monkey home page Code Monkey logo

sample-app-next-js's Introduction

Kontent.ai multi-web sample application template

Contributors Forks Stargazers Issues MIT License

Demo

Ficto Healthtech

Vercel

Ficto Imaging

Vercel

Ficto Surgical

Vercel

Getting Started

To run the app yourself you will need a clone of the Kontent.ai project. You can create it right from the Kontent.ai UI as a multi-brand sample project. You can learn more about the sample project in our documentation.

Deploy

Deploy with Vercel

Deploy on Netlify

Init project from command line

Execute create-next-app with npm or Yarn to bootstrap the example:

npx create-next-app --example https://github.com/kontent-ai/sample-app-next-js sample-app-next-js
# or
yarn create next-app --example https://github.com/kontent-ai/sample-app-next-js sample-app-next-js

Preview mode

The app uses the Next's preview mode to display Kontent.ai preview data on the site.

All the features, including preview urls, Web Spotlight and multiple previews are configured automatically when the project is generated. Next.js preview mode is also toggled whenever you view content via Web Spotlight or Preview button.

If you open the app outside of Kontent.ai, it will by default show the published content. To enable the preview mode, visit the /api/preview route and provide the following query parameters:

  • secret - This prevents unauthorised access to the preview data. Default value is mySuperSecret.
  • slug - This defines where should the app redirect you once the preview mode is enabled (e.g. /).
  • type - This must be the codename of the content type that the item represented by slug is based on. It can be either page or web_spotlight_root.

An example might look something like this: /api/preview?secret=mySuperSecret&slug=about-us&type=page. To exit the preview mode, visit the route /api/exit-preview. No query parameter is necessary, but you can provide callback with a path to redirect to once the preview mode is disabled.

The preview mode leverages cookies, so when you open the app in preview (e.g. from Kontent.ai) and then open it again (e.g. in a different tab), the second instance will remain in preview, as long as the cookies are present. You can clear cookies manually or visit /api/exit-preview which removes them as well.

Code development

Environment variables

  1. Set up environment variables

    • Copy the .env.local.template file in this directory to .env.local (which will be ignored by Git):

      cp .env.local.template .env.local
    • Fill in all the necessary variables in .env.local based on the comments.

  2. Run the development server:

    npm run dev
    # or
    yarn dev

๐ŸŽ‰ Open http://localhost:3000 with your browser to see the result.

Running the app in web spotlight

If you want to use your app inside web spotlight, you will need to run the app under the https scheme.

To run the app under the https scheme you can use one of the following methods:

  • Run npm run https:dev to run the app in the development mode and a proxy server proxying https://localhost:3001 to http://localhost:3000.
    • The proxy will use a self-signed certificate which might not work in some browsers.
    • The proxy is run using the local-ssl-proxy package.
    • The command requires the ports 3001 and 3000 to be free, otherwise it fails. If you want to use different ports, you will need to run the proxy (npm run https:proxy) and the app npm run dev yourself.
  • Run npm run https:proxy to create a proxy as above without running the app (you are expected to run the app separately).
    • You can use this command with a custom trusted certificate like this npm run https:proxy -- --key localhost-key.pem --cert localhost.pem. See the package documentation for more details
    • You can also change the source and/or target port (e.g. npm run https:proxy -- --source 3002 --target 4000)
  • Write your own server.
  • Use Ngrok or a similar tool.

You can adjust the homepage by editing pages/[envId]/index.tsx. The page auto-updates as you edit the file.

To generate new models from Kontent.ai data, just run npm run generateModels. Make sure you have environment variables filled in properly.

Circular reference handling

Next.js data fetching functions convert objects to JSON format. Since JSON doesn't support circular data, this can potentially cause crashes in situations where objects reference each other, such as with linked items or rich text elements. To avoid this, the application uses the flatted package to implement two helper functions: stringifyAsType and parseFlatted, which allow for safe conversion of circular structures into a string form in getStaticProps and then accurately reconstruct the original objects from that string.

Use codebase as a starter

โš  This project is not intended as a starter project. It is a sample of a presentation channel showcasing Kontent.ai capabilities. The following hints help you use this code as a base for presentation channel for your project like a boilerplate. By doing it, you are accepting the fact you are changing the purpose of this code.

The app contains code to dynamically handle different Kontent.ai projects (e.g. the environment route prefix). To adjust the code to be used for a single project as a starter, you should remove the logic that is used solely for showcasing the sample project during evaluation.

Below are some of the parts responsible for handling different Kontent.ai projects that need adjustment in case of transforming the code into a single-project setup:

  • middleware.ts - Gets the Kontent.ai environment ID and stores it in a cookie. For single-project setup, a single environment variable should be used to store the environment ID.
  • pages/callback.tsx & pages/getPreviewApiKey.ts & lib/constants/auth.ts - Responsible for exchanging preview API keys for specified environment. For single-project setup, a single environment variable should be used to store the preview API key.
  • pages/[envId] - Folder representing the dynamic segment, passing the environment ID for pages. For single-project setup, remove the folder and move its content one level up.

Commands

"scripts": {
"generateModels": "tsc --project scripts/tsconfig.json && node scripts/build/generateModels.mjs",
"dev": "next dev",
"lint": "next lint",
"lint:fix": "next lint --fix",
"build": "next build",
"export": "next export",
"start": "next start",
"test": "jest"
},
"dependencies": {
"@heroicons/react": "^2.0.17",

sample-app-next-js's People

Contributors

ivankiral avatar jirilojda avatar kontent-ai-bot avatar pokornyd avatar simply007 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

sample-app-next-js's Issues

How to start (readme) is incomplete

Brief bug description

I tried to run the site for the first time according to the description in the readme,

  1. Copied the environment variables
  2. Started the site with npm run dev

And it ended with an error that the preview API key is missing, so I had to provide it and run it again

It would be nice to make it more clear which environment variables are mandatory

BTW: Not sure how the preview is enabled, and if running with just start would use live or not. Also not clear from the readme. If not, it may not make sense to even provide project ID by default

Refactor zigzag layout with CSS

Motivation

Current implementation of zigzag layout relies upon passing an incrementing index of a fact/stack component, combined with modulo to style even and odd components respectively. This is rather cumbersome and could be potentially rewritten using tailwind built-in modifiers of the same name.

Proposed solution

Rewrite zigzag layout using tailwind functionality, rather than passing down numeric prop.

Additional context

Add any other context, screenshots, or reference links about the feature request here.

FICTO IMAGING - content disapears when clicked combined solutions

Brief bug description

FICTO IMAGING - content disapears when clicked combined solutions

Repro steps

  1. Go to 'https://ficto-imaging.sample.kontent.ai'
  2. Click on 'Combined solutions'
  3. See nothing

Expected behavior

The content and website parts should be visible

Test environment

  • Platform/OS: [e.g. .NET Core 2.1, iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Additional context

Add any other context about the problem here.

Screenshots

image

Failing website link on github

Hi.

Just a small info / request not related to the code itself, but to your project hosted on github.
While I have seen updates to the readme, just recently, a very prominent link is not working and probably needs an update too:

The website link of the github repo.
image

Looking forward to check out this repo further!

Cheers Fabian

https:dev starts with wrong proxy

Brief bug description

When starting the app with https:dev and localhost:3000 taken, it creates a wrong HTTPS proxy

What went wrong?

Repro steps

Have localhost:3000 used by another process

Start https:dev

It makes proxy to localhost:3000 but later starts the site on localhost:3002 (or whatever is available)

image

Expected behavior

The proxy is created to a port that is actually used to run the site

Test environment

Windows

Is backup data of this project available ?

Hi again

I am checking out this repo, to see how well Kontent.ai and Next.js would work out for us!
It looks like this is one of the most up to date repos in this context, and looks like it has quite some features covered.
Do you suggest this or maybe an other repo for checking out the possibilities of Kontent.ai and Next.js setup?

Until now I just tried out the Repo from Vercel, which worked out nicely but is probably much more basic as this setup here.

I have seen an other repo from you guys, but probably also not that big and maybe outdated:

If this repo is the right place:
I found the following in your readme:

TBD - raise the issue, if you want the project backup

Is it possible to get a backup of the Kontent.ai project to import it?
I guess I could import that then over your backup-manager tool - like I did with the Vercel example...

Cheers

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.