Skip to content
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

How can I load all components by fetch result with nextjs? #34

Open
Evansy opened this issue May 25, 2021 · 1 comment
Open

How can I load all components by fetch result with nextjs? #34

Evansy opened this issue May 25, 2021 · 1 comment

Comments

@Evansy
Copy link

Evansy commented May 25, 2021

I wan to load some components by a fetch result, such as:

// registryComponent.js
import { createRequires, fetchRemoteComponent, getServerSideProps as getProps } from "@paciolan/remote-component";
import dynamic from "next/dynamic";

import config from "./remote-component.config";
const requires = createRequires(config.resolve);

export function registryComponents(urls: { url: string; name: string }[] = []): Record<string, any> {
  const compoents: Record<string, any> = {};
  for (let i = 0; i < urls.length; i++) {
    const c = urls[i];
    compoents[c.name] = dynamic(() => fetchRemoteComponent({ url: c.url, requires }));
  }

  return compoents;
}

export function getComponentRequest(
  urls: { url: string; name: string }[] = [],
  data: any,
  context?: any
): Promise<unknown>[] {
  const requests: Promise<unknown>[] = [];
  for (let i = 0; i < urls.length; i++) {
    const c = urls[i];
    requests.push(
      getProps({
        url: c.url,
        requires,
        context: { ...context, data },
      })
    );
  }

  return requests;
}
// next.js Page
import React from "react";

import { registryComponents, getComponentRequest } from "./registryComponent";

class HelloWorld extends React.Component {
  comps = {};

  static async getInitialProps(ctx: any): Promise<any> {
    const data = { id: 1 };
    const urls = await fetch("http://mockhttp.cn/mock/components", { method: "GET" }).then((res) => res.json());
    // return components's request Promise
    const requests = getComponentRequest(urls, data, ctx);
    // return components
    const dyComp = registryComponents(urls);
    // waiting request finish.
    const compData = await Promise.all(requests);
    return { jsonData: urls, compData, dyComp };
  }

  render(): JSX.Element {
    const { jsonData, compData, dyComp } = this.props;
    return (
      <div>
        {dyComp &&
          Object.keys(dyComp).length &&
          jsonData &&
          jsonData.map((item: any, index: number) => {
            const DynamicComponent = dyComp[item.name];
            return <DynamicComponent key={item.name} data={compData[index]} />;
          })}
      </div>
    );
  }
}

The question is in nextjs getInitialProps dyComp can't be translate to JSON. so it can not work.

on browser console, console.log(dyComp) ==> {table: {}, button: {}}

Do you have a good idea?

@joelnet
Copy link
Member

joelnet commented Aug 26, 2021

Sorry for delayed response. Next.js support is still experimental so don't yet have a clear recommended way of using the Remote Component within Next.js.

But we are using Next.js for some projects, so there may be some official support coming soon.

@joelnet joelnet added the enhancement New feature or request label Aug 26, 2021
@joelnet joelnet removed the enhancement New feature or request label Feb 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants