Coder Social home page Coder Social logo

Comments (8)

pchokanson avatar pchokanson commented on June 4, 2024 1

I was able to get this to work with a little tweaking:

  1. The components you're using Cesium in need to be client-side. They have an example of how to do this.

  2. You can set the webpack settings in next.config.js by doing most of the steps in the guide. I ended up with the following:

module.exports = {
    webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
        try{
            config.externals.cesium = "Cesium";
        } catch {
            config.externals = {
                cesium: "Cesium",
            };
        }

        if(isServer){
            config.plugins.push(new CopyWebpackPlugin([{
                from: path.join(__dirname, (dev ? 
                    "node_modules/cesium/Build/CesiumUnminified/" :
                    "node_modules/cesium/Build/Cesium/")),
                to: path.join(__dirname, "static/Cesium"),
            }, ]));
        }
        config.plugins.push(new HtmlWebpackPlugin({ }));
        config.plugins.push(new webpack.DefinePlugin({
            CESIUM_BASE_URL: JSON.stringify("static/Cesium"),
        }));
        return config;
    },
    ...
};

Note that this copies to the static directory that's used by next.js for serving assets directly. You should probably use public, but I haven't switched over yet.

  1. I wasn't trivially able to use the webpack configuration to pull in the assets, so I just added them directly to the page that actually needs my component. There is probably a better solution there, but it hasn't been worth my time to find.

That said, this has still been a bit fragile, but I'm not sure exactly where in the stack is actually causing the trouble.

from resium.

rot1024 avatar rot1024 commented on June 4, 2024 1

Good news, I succeeded in getting Resium to work with Next.js!

from resium.

rot1024 avatar rot1024 commented on June 4, 2024

I'm trying to run Resium in Next.js. but it seems to be hard...

Copying Cesium files is OK:

// next.config.js
const path = require('path');
const webpack = require('webpack');
const CopyPlugin = require("copy-webpack-plugin");

module.exports = (config) => ({
  ...config,
  webpack(conf, { isServer }) {
    conf.externals = { cesium: "Cesium" };
    if (!isServer) {
      conf.plugins.push(
        new CopyPlugin([{
          from: path.join(__dirname, "node_modules/cesium/Build/Cesium"),
          to: path.join(__dirname, "static/cesium")
        }]),
	new webpack.DefinePlugin({
          CESIUM_BASE_URL: JSON.stringify("/static/cesium")
        }),
      );
    }
    return conf;
  }
});

But the following code does not work.

// pages/index.js
// does not work

import React from "react";
import Head from 'next/head';
import dynamic from 'next/dynamic';

function newScript(src) {
  return new Promise(function(resolve, reject){
    var script = document.createElement('script');
    script.src = src;
    script.addEventListener('load', function () {
      resolve();
    });
    script.addEventListener('error', function (e) {
      reject(e);
    });
    document.body.appendChild(script);
  })
};

const Resium = dynamic(() => import('resium'), {
  ssr: false,
  render: (props, mod) => {
    console.log(mod); // {} is displayed
    return (
      <mod.Viewer full />
    );
  }
});

class Home extends React.Component {
  
  state = { resium: false };

  componentDidMount() {
    // Cesium must be loaded before loading Resium
    newScript("/static/cesium/Cesium.js").then(() => {
      this.setState({ resium: true });
    }).catch(err => console.error(err))
  }

  render() {
    return (
      <div>
        <Head>
          <link rel="stylesheet" href="/static/cesium/Widgets/widgets.css" />
        </Head>
        {this.state.resium && (
          <Resium />
        )}
      </div>
    );
  };
};

export default Home;

from resium.

SHoar avatar SHoar commented on June 4, 2024

Next.js is used for server side rendering (SSR) - wouldn't you be doubling the amount of work that Cesium needs to do?

from resium.

darcismoreno avatar darcismoreno commented on June 4, 2024

Hi guys! Going through the same process... @rot1024 @TobiasSchaeuble did you guys find a workaround to this?

from resium.

nrgapple avatar nrgapple commented on June 4, 2024

Hmm is there any other solution for nextjs + resium currently?

from resium.

nrgapple avatar nrgapple commented on June 4, 2024

I wasn't trivially able to use the webpack configuration to pull in the assets, so I just added them directly to the page that actually needs my component. There is probably a better solution there, but it hasn't been worth my time to find.

Do you have an example of how to import directly?

from resium.

Jeetpal1 avatar Jeetpal1 commented on June 4, 2024

Hey guys,
I am confused about deploying the Nextjs + Resium app on Vercel.
There seems to be an issue when deployed.
Basically, I followed this guide (https://resium.reearth.io/installation#2-nextjs ), also I changed "postinstall": "symlink-dir node_modules/cesium/Build/Cesium public/cesium" to "postinstall": "cp -r node_modules/cesium/Build/Cesium public/cesium", which bypasses the issue (#529 ). Following error occurs after vercel deploy command successfully being completed and I go to the generated url.
For example:
image

from resium.

Related Issues (20)

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.