Giter Club home page Giter Club logo

piral-nextjs-sample's Introduction

Piral Next.js Example

This example shows how an existing application using Next.js could be changed to be a pilet.

The application is still capable of running as a Next.js app, while also being buildable as a pilet.

Remark: This approach is valid for a single Next.js pilet. If you have multiple Next.js applications that you want to run in the same Piral instance then sharing the next/head etc. components from the app shell would be recommended.

Required Changes

We've installed the piral-cli and the respective app shell (in this case sample-piral). We've added the necessary fields to the package.json.

package.json

Most notably we did add:

  "piral": {
    "name": "sample-piral",
    "externals": [
      "sample-piral",
      "react",
      "react-router-dom"
    ]
  },

To improve the experience even more we've also declared peer dependencies.

  "peerDependencies": {
    "react": "*",
    "react-router-dom": "*",
    "sample-piral": "*"
  },

Finally, it makes sense to also add scripts for building the pilet.

  "scripts": {
    "dev": "next",
    "build": "next build",
    "build-pilet": "pilet build",
    "debug-pilet": "pilet debug",
    "start": "next start"
  },

src/index.js

A new src/index.js file was added. This one performs all the registrations.

We've also added a tile to link to /sample for demonstration purposes.

Reference React

Every source file did not reference React explicitly. This is, however, usually necessary. Potentially, a .babelrc could be used for this. In this example we've just added

import * as React from 'react';

on top of each file.

React Router

Since the next router is not compatible with the React router I've also made a slight change here. I've added an alias field to the package.json. This tells Parcel to resolve some package differently. In our case next/link will be resolved to ./components/Link.js. This element will also prefix all used links with "/sample" such that /contact would be /sample/contact.

  "alias": {
    "next/link": "./components/Link.js"
  },

piral-nextjs-sample's People

Contributors

florianrappl avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

piral-nextjs-sample's Issues

How to use as App Shell?

Hi, I'm trying to use Nextjs as a shell app. Here is the sample page:

import { createInstance, LoadingIndicatorProps, Piral, SetComponent } from "piral-core";
import React from "react";
import { configForServerRendering } from "piral-ssr-utils";

export function createAppInstance(){
	return createInstance(
		configForServerRendering({
			requestPilets(){
				return fetch("https://feed.piral.cloud/api/v1/pilet/empty")
				.then((res) => res.json())
				.then((res) => {
					console.log(res);
					return res.items;
				});
			},
			plugins: [],
		}),
	);
}

const Loader: React.FC<LoadingIndicatorProps> = () => (
	<div className="app-center">
		<div className="spinner circles">Loading ...</div>
	</div>
);

const DashboardContainer: React.FC = ({ children }) => <div className="tiles">{children}</div>;

const Layout: React.FC = ({ children }) => (
	<div className="app-container">
		<div className="app-header">
			<h1>Cross Framework Sample</h1>
		</div>
		<div className="app-content">{children}</div>
		<div className="app-footer">
			For more information or the source code check out our{" "}
			<a href="https://github.com/smapiot/piral">GitHub repository</a>.
		</div>
	</div>
);


const PiralInstance = ({ children }: { children?: React.ReactChildren }) => {
	const instance = createAppInstance();

	return <Piral instance={instance}>
		<SetComponent name="LoadingIndicator" component={Loader}/>
		<SetComponent name="Layout" component={Layout}/>
		{children}
	</Piral>;
};


export default PiralInstance;

but without proper workarounds code breaks with: ReferenceError: sessionStorage is not defined.

there is any working example with Next.js as App Shell? What am I doing wrong? :(

Thanks so so much in advance for your work

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.