Giter Club home page Giter Club logo

builder-shopify-hydrogen's Introduction

Builder.io + Shopify Hydrogen - headless visual page building example

🚧 🚧 🚧

This starter is deprecated.

This starter works for Hydrogen v1 only. For Hydrogen V2, visit our integration guide and click on the Remix tab in the example code box. There is also a Remix example project that can be found here

Example repo using Builder.io for drag and drop page building with Shopify hydrogen

Demo

Get Started

  • Clone this project: git clone https://github.com/BuilderIO/builder-shopify-hydrogen
  • Install dependencies: cd builder-shopify-hydrogen; yarn
  • Replace any builder.init calls with your API key: https://www.builder.io/c/docs/using-your-api-key#finding-your-public-api-key
  • Run the server: yarn dev
  • Create a free account at builder.io
  • Go to builder.io/models, choose the page model and change the Editing URL to http://localhost:3000
  • Create a new page with url /builder/testing
  • Explore the drag and drop editor
  • Hit publish, then go to http://localhost:3000/builder/testing and see your page!

Explore more in the Builder.io developer docs

Why Builder.io?

Hardcoding layouts for frequently changing content bottlenecks your team and makes releases messy

Using an API-driven UI allows you to:

  • Decouple page updates from deploys
  • Schedule, a/b test, and personalize via APIs
  • Reduce code + increase composability

Who uses Builder.io?


How is the content structured?

In Builder, content is structured in models, and customized with custom fields and targeting

  • Builder pages - full drag and drop control between your site's header and footer. Try it out
  • Builder sections - make a part of a page visually editable in Builder and use our targeting and scheduling to decide who sees what. Try it out
  • Builder data - fetch structured data from Builder and use it anywhere in your application (e.g. menu items, structured pages). Try it out

Read more about how builder works here

See here for examples on how to structure a site with Builder

Getting started

Requirements:

  • Node v14+
  • Yarn
yarn
yarn dev

Remember to update shopify.config.js with your shop's domain and Storefront API token!

Previewing a production build

To run a local preview of your Hydrogen app in an environment similar to Oxygen, build your Hydrogen app and then run yarn preview:

yarn build
yarn preview

Building for production

yarn build

Then, you can run a local server.js using the production build with:

yarn serve

Structuring your site

There are a lot of ways you can use Builder for your site. Some of the main questions you'll want to ask yourselves - what on your site should be in your code vs in Builder.

As a general rule, parts of your site that should be managed by non developers should probably be in Builder. Parts that are complex with a lot of code, should probably be in your codebase. Using custom components in your Builder content can help you strike a good balance here as well

Here are some examples we recommend for how to structure various pages on your site, for instance for a headless commerce site:

examples on how to structure your site

How the Builder.io platform works

How it works

Data models, components, SEO, and more

Builder.io gives you a ton more power and control than just page building. Check our guides on

Additional framework support:

As well as some handy power features like:

Join the community!

Questions? Requests? Feedback? Chat with us in our Discord!

Troubleshooting and feedback

Problems? Requests? Open an issue. We always want to hear feedback and interesting new use cases and are happy to help.

builder-shopify-hydrogen's People

Contributors

dkendal avatar gustavocodess avatar mrkoreye avatar steve8708 avatar teleaziz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

builder-shopify-hydrogen's Issues

BuilderComponent Hydrogen SSR

I'm trying to use the BuilderComponent as SSR so I can be able to fetch Shopify Product data from the handle in a Builder Custom Component.

I've seen this issue but I was not able to find any follow-up on this. Is there any update on it?

Shopify/hydrogen#631

Oxygen production build fails - TypeError: Cannot read properties of null (reading 'parse')

When attempting to deploy to Oxygen, the system fails due to the following error:

Cannot read properties of null (reading 'parse')

I've installed a vanilla copy of hydrogen and I started copying over code line by line. When I included the builder client component, the builder.init method causes this error to be thrown. When I run hydrogen in dev mode, it renders just fine. But when it builds for production (as it needs to in order to deploy to Oxygen) the script cannot proceed past that point so it cannot complete a successful deployment.

Updating to new Hydrogen with Remix

There is a new version of Hydrogen that uses Remix. I was trying to add the Builder SDK similar to what you do on this repository, but the file structure is different, and I need some help. Are you planning to update this repository to use the most current version of Hydrogen, or can you direct me in the right direction for implementing the Builder SDK correctly?

Builder is not creating pages

Hi, after creating the /builder/testing page, I can use the page in the builder customizer, but when I hit publish and go to the URL http://localhost:3000/builder/test, I get a "We lost this page" message.

This is the console log:

  > Local: http://localhost:3000/
  > Network: use `--host` to expose

GET streaming SSR     200 1559.16 ms http://localhost:3000/
GET streaming SSR     200 1052.57 ms http://localhost:3000/
GET Server Components 200 204.57 ms  {"pathname":"/collections/freestyle","search":""}
{ data: undefined }
GET streaming SSR     200 208.31 ms  http://localhost:3000/builder/testing?builder.space=c77a2e1d825941f8b48b819ffd78b779&builder.cachebust=true&builder.preview=page&builder.noCache=true&__builder_editing__=true&builder.overrides.page=9361ad53ee674619bdc2ff0ffa736331&builder.overrides.9361ad53ee674619bdc2ff0ffa736331=9361ad53ee674619bdc2ff0ffa736331&builder.overrides.page:/builder/testing=9361ad53ee674619bdc2ff0ffa736331&builder.frameEditing=page
3:00:49 PM [vite] ✨ new dependencies optimized: @builder.io/react, @builder.io/widgets
3:00:49 PM [vite] ✨ optimized dependencies changed. reloading
3:00:52 PM [vite] page reload node_modules/@shopify/hydrogen/vendor/react-server-dom-vite/esm/react-server-dom-vite.js
3:00:52 PM [vite] page reload node_modules/@shopify/hydrogen/vendor/react-server-dom-vite/esm/react-server-dom-vite.js (x2)
{ data: undefined }
{ data: undefined }
GET streaming SSR     200 1217.15 ms http://localhost:3000/
GET streaming SSR     200 1238.90 ms http://localhost:3000/builder/testing?builder.space=c77a2e1d825941f8b48b819ffd78b779&builder.cachebust=true&builder.preview=page&builder.noCache=true&__builder_editing__=true&builder.overrides.page=9361ad53ee674619bdc2ff0ffa736331&builder.overrides.9361ad53ee674619bdc2ff0ffa736331=9361ad53ee674619bdc2ff0ffa736331&builder.overrides.page:/builder/testing=9361ad53ee674619bdc2ff0ffa736331&builder.frameEditing=page
GET streaming SSR     200 1250.24 ms http://localhost:3000/builder/testing?builder.space=c77a2e1d825941f8b48b819ffd78b779&builder.cachebust=true&builder.preview=page&builder.noCache=true&__builder_editing__=true&builder.overrides.page=9361ad53ee674619bdc2ff0ffa736331&builder.overrides.9361ad53ee674619bdc2ff0ffa736331=9361ad53ee674619bdc2ff0ffa736331&builder.overrides.page:/builder/testing=9361ad53ee674619bdc2ff0ffa736331&builder.frameEditing=page
GET streaming SSR     200 1297.48 ms http://localhost:3000/
{ data: undefined }
GET streaming SSR     200 394.11 ms  http://localhost:3000/builder/testing

Uncaught Error: EvalError: Code generation from strings disallowed for this context

I am attempting to deploy our project through Shopify Oxygen with GitHub Actions and am getting the following error from the @builder.io/react package due to the use of eval...

Uncaught Error: EvalError: Code generation from strings disallowed for this context

Here is the full error for completeness:

🛠️ Building the application... 
yarn run v1.22.19
$ shopify hydrogen build
[STARTED] Building client code
Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification
Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification
Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification
Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification
[TITLE] Built client code
[SUCCESS] Built client code
[STARTED] Building worker code
Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification
Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification
Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification
Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification
[TITLE] Built worker code
[SUCCESS] Built worker code
Done in 22.01s.
✅ Done in 22.16s.

🚀 Uploading deployment files... 
🛑 InvalidScriptError: retrying will not succeed. The following error must be actioned:

Uncaught Error: EvalError: Code generation from strings disallowed for this context
  at worker.mjs:2203:68269
Error: Process completed with exit code 1.

BuilderComponent pages render extremely slowly when running locally

When running locally, the BuilderComponent from @builder.io/react renders Builder pages extremely slowly and causes the dev server to hang. In our case, this also makes using the Builder content editor pretty difficult.

That said, our project is deployed through Github actions and Oxygen, and the deployed pages load quickly (as expected)

We are using a project based off of the Shopify Hydrogen demo store: https://github.com/Shopify/hydrogen/tree/2023-01/templates/demo-store

The following shows an example use case:

import {builder, BuilderComponent, BuilderContent} from '@builder.io/react';
import {Await, useLoaderData} from '@remix-run/react';
import builderConfig from '../../../builderConfig.json';

builder.init(builderConfig.apiKey);

const BUILDER_MODEL = 'page';

export async function loader() {
  const builderContent = await builder
    .get(BUILDER_MODEL, {
      userAttributes: {
        urlPath: '/',
      },
    })
    .toPromise();

  if (!builderContent) {
    throw new Response('Not found', {status: 404});
  }

  return builderContent;
}

// this gives full compatibility to BuilderContent type and Remix starter
// See: https://github.com/BuilderIO/builder/issues/1387#issuecomment-1397442797
type BuilderContentRemix = Omit<BuilderContent, 'variations' | 'data'>;

export default function Homepage() {
  const builderContent: BuilderContentRemix =
    useLoaderData<BuilderContentRemix>();

  return (
    <Suspense>
      <Await resolve={builderContent}>
        <BuilderComponent content={builderContent} model={BUILDER_MODEL} />
      </Await>
    </Suspense>
  );
}

ERROR: Field 'metafields' is missing required arguments: identifiers

I have performed all setup steps as per the readme. Running node v16.13.2. When I try to load the page in the builder.io editor or on its own, I receive the error message above. I haven't been able to find any existing information or other cases pertaining to this error.

Please advise. Thanks!

image

Cannot use the builder/react sdk in the new version of shopify-hydrogen using Remix

Shopify-Hydrogen has recently released a new version using Remix (https://github.com/Shopify/hydrogen/tree/2023-01/templates/demo-store)

The following error is generated when attempting to use anything from the @builder.io/react sdk in any of the routes. The only place I seem to be able to use @builder.io/react successfully is in the entry.client.tsx file.
evalError

Our project was created using the newest template from Shopify-Hydrogen. In my case, this error is generated when using useIsPreviewing() or the BuilderComponent

As an example, the error is generated in the component shown below:

import {BuilderComponent, useIsPreviewing} from '@builder.io/react';
import {BuilderContent} from '@builder.io/sdk';
import {Suspense} from 'react';
import tw from 'tailwind-styled-components';
import {NotFound} from '../NotFound';

export const PaxBuilderPage = ({
  builderContent,
  builderModel,
}: {
  builderContent: Omit<BuilderContent, 'variations' | 'data'>;
  builderModel: string;
}) => {
  const isPreviewingInBuilder = useIsPreviewing();

  if (!builderContent && !isPreviewingInBuilder) {
    return <NotFound />;
  }

  return (
    <Suspense>
      <BuilderContentContainer>
        <BuilderComponent content={builderContent} model={builderModel} />
      </BuilderContentContainer>
    </Suspense>
  );
};

const BuilderContentContainer = tw.div`
  flex
  h-fit
  flex-col
`;

The above PaxBuilderPage component is being used in our site's Homepage (route = '/') as shown below. That said, the same error will still occur when the BuilderComponent is imported from @builder.io/react directly into the Homepage

import {builder, BuilderContent} from '@builder.io/sdk';
import {Await, useLoaderData} from '@remix-run/react';
import builderConfig from '../../../builderConfig.json';
import {PaxBuilderPage} from '../../components/builder/PaxBuilderPage';

builder.init(builderConfig.apiKey);

const BUILDER_MODEL = 'page';

export async function loader() {
  const builderContent = await builder
    .get(BUILDER_MODEL, {
      userAttributes: {
        urlPath: '/',
      },
    })
    .toPromise();

  if (!builderContent) {
    throw new Response('Not found', {status: 404});
  }

  return builderContent;
}

// this gives full compatibility to BuilderContent type and Remix starter
// See: https://github.com/BuilderIO/builder/issues/1387#issuecomment-1397442797
type BuilderContentRemix = Omit<BuilderContent, 'variations' | 'data'>;

export default function Homepage() {
  const builderContent: BuilderContentRemix =
    useLoaderData<BuilderContentRemix>();

  return (
    <Await resolve={builderContent}>
      <PaxBuilderPage
        builderContent={builderContent}
        builderModel={BUILDER_MODEL}
      />
    </Await>
  );
}

Builder is not woking on other pages other than the /builder folder. How do we fix this

Builder is not working on other pages other than the /builder folder. How do we fix this?

Builder.io only works on the builder/ folder, so any pages out side of this folder would not work, unable to edit normal pages such as /pages/about us or homepage as well.

How would I fix this or what code would I need to input to [handle].server.tsx ? I cant publish or edit page as the page shows 404 for new pages made, or it will show the page for the pages on shopify but unable to edit them,

Any help is appreciated.

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.