All posts
Platform migrations

HubSpot CMS to Next.js: Custom Front End, HubSpot Back Office

HubSpot to Next.js: rebuild your site with a custom front end while HubSpot stays the back office. Forms API, tracking and lead routing patterns for B2B.

By Mehdi Hassan7 min read

The short answer

Moving from HubSpot to Next.js means rebuilding the site as a custom front end while HubSpot stays your CRM. Content moves to a headless CMS, forms submit to the HubSpot Forms API with the visitor's tracking cookie, the tracking code records page views on every route change, and HubSpot workflows keep routing leads to sales.

Key takeaways

  • Submit custom forms server-side to the HubSpot Forms API with the hutk cookie, page URL and page name.
  • Use existing form IDs so HubSpot workflows, lists and notifications keep working unchanged.
  • Next.js navigates client-side, so push a page view to HubSpot on every route change.
  • Let HubSpot workflows do the routing; add server-side logic only where it clearly helps.
  • Move content to a headless CMS; keep HubSpot for contacts, email and sales.

Part of Leaving HubSpot CMS: Where to Move Your Website and Keep the CRM

For B2B teams with developers, Next.js gives the most control over a marketing site: custom forms, fast rendering, any CMS and any integration. HubSpot stays in place as the back office for contacts, email, workflows and sales. The work that decides whether this goes well is the connection between the two. This guide covers the patterns we use for forms, tracking and lead routing. For what else changes when you leave HubSpot's website tools, start with our hub on leaving HubSpot CMS while keeping the CRM.

What does a HubSpot to Nextjs rebuild involve?

A HubSpot to Next.js rebuild involves four layers: a Next.js front end, a headless CMS for content, a forms and tracking layer that talks to HubSpot, and redirects for every changed URL. HubSpot themes and HubL templates don't carry over, so the front end is rebuilt as React components.

Layer

On HubSpot CMS

On Next.js

Pages and templates

HubSpot theme, modules, HubL

React components and routes

Content

HubSpot pages, blog, HubDB

Headless CMS such as Sanity or Payload

Forms

Native HubSpot forms

HubSpot embeds or custom forms via the Forms API

Tracking

Automatic

Tracking code plus route-change page views

Personalization

Smart content

Server-side logic or a personalization tool

Redirects

HubSpot URL redirects

Next.js config or the host's redirects

For content, most teams pick a headless CMS so marketers can still edit without a developer; our explainer on what a headless CMS is covers the basics, and our headless CMS migration guide compares the options. It's also possible to keep the HubSpot blog and read it through HubSpot's CMS API, but that keeps you paying for Content Hub, which is usually what you're trying to stop.

How do you connect custom Next.js forms to the HubSpot Forms API?

You connect custom Next.js forms to the HubSpot Forms API by posting form data to a server route or server action, which forwards it to HubSpot's form submission endpoint along with the existing form's ID, the visitor's hubspotutk cookie, the page URL and the page name. Sending the cookie is what links the submission to the visitor's history.

  1. Keep the HubSpot form

    Use the existing form in HubSpot as the target, with the same field internal names. Workflows tied to it keep working.

  2. Build the form in React

    Match HubSpot's field names, add validation, and follow the basics in our accessible forms guide.

  3. Submit server-side

    Post to a Next.js route handler or server action, not straight from the browser, so you can add spam checks and keep logic private.

  4. Add context

    Read the hubspotutk cookie and include it with the page URL and page title in the submission's context.

  5. Include consent

    If the form collects marketing consent, send the consent options HubSpot expects, matching the form's settings.

  6. Handle errors

    Show a clear message if HubSpot rejects the submission, and log failures so no lead disappears silently.

For simple forms, HubSpot's own embed code still works in Next.js, loaded with a script component on the pages that need it. We use custom forms where design, multi-step flows or validation justify the extra build.

How do you set up HubSpot tracking in Next.js?

You set up HubSpot tracking in Next.js by loading the tracking script once in the root layout, then pushing the new path and a page view to HubSpot's tracking queue whenever the route changes. Next.js navigates between pages on the client, so without that step HubSpot only records the first page of each visit.

  • Load once. Add the tracking script in the root layout with a script component that loads after the page is interactive.
  • Track route changes. In a small client component, watch the pathname and, on each change after the first, push setPath and trackPageView to HubSpot's queue.
  • Respect consent. If you use your own consent tool, only load or enable tracking after consent.
  • Verify. Visit five pages in a row and confirm all five appear on the visitor's timeline in HubSpot.
Hypothetical example: a double-counted page view

For example, suppose a team adds the route-change tracker and also lets the tracking script record the initial load. If the tracker fires on the first render as well, every visit's landing page is counted twice, and page-view reports look inflated.

The fix is to skip the tracker's first run and only push page views on later path changes. The team confirms it by landing on one page, waiting, and checking that HubSpot shows a single view.

What lead routing patterns work best with HubSpot and Next.js?

The lead routing pattern that works best for most B2B teams is to submit every form to HubSpot and let HubSpot workflows route it. Add routing logic in Next.js only when the visitor needs a different experience immediately, such as showing a booking calendar to a qualified lead.

Pattern

How it works

Use it when

Workflow routing

Form goes to HubSpot; workflows assign owner and notify

Default for most teams

Qualify then book

Server checks answers, shows a meetings embed to qualified leads

Sales wants faster demos for good-fit leads

Spam filter first

Server runs a bot check or honeypot before forwarding

You get form spam

Product signup sync

App writes contacts via the CRM API

Signups happen in your product, not a marketing form

Enrich then submit

Server adds data such as company domain before submitting

Routing depends on firmographic fields

Keep routing rules in one place where possible. If half the logic lives in Next.js and half in HubSpot workflows, nobody can explain why a lead went where it did. HubSpot is the better home for rules that marketing and sales ops change often.

How do marketers edit a Next.js site after leaving HubSpot?

Marketers edit a Next.js site through the headless CMS, using page-builder blocks and live preview, so they can publish without a developer. Without that setup, every change becomes a ticket, and the team misses HubSpot's page editor quickly.

Give marketers reusable sections, a form block where they pick a HubSpot form by name, and SEO fields on every page. Set up preview so they see changes on the real page before publishing; our guide to live preview and visual editing explains the options. If the team would rather design visually, our guide to keeping Framer's design and moving content to a headless CMS shows a lighter alternative.

Is Next.js the right destination after HubSpot CMS?

Next.js is the right destination after HubSpot CMS when you have developers to maintain it and you need custom forms, complex routing, programmatic pages or integrations that visual builders struggle with. If marketers will make most changes and the site is mostly standard pages, Webflow or Framer is usually simpler.

Frequently asked questions

Do I need a HubSpot API key to submit forms from Next.js?
HubSpot's standard form submission endpoint accepts submissions for a form without a private access token, because it behaves like a public form. HubSpot also offers an authenticated version that uses a private app token. Either way, submit from the server rather than the browser, so you can add spam protection and keep any token out of client code.
Can I use HubSpot's embedded forms in a Next.js site?
Yes. Load HubSpot's forms script with a script component and create the form inside a container on the pages that need it. It's the quickest route and keeps form logic in HubSpot. Custom React forms with the Forms API give more design control and validation, so many teams use embeds at launch and replace key forms later.
Will HubSpot chat and meetings work on a Next.js site?
Yes. HubSpot chatflows load through the tracking code, so they appear once it's installed in the root layout. Meeting pages can be embedded with HubSpot's meetings embed. Check that chatflow targeting rules use the new URLs, and test that the chat widget behaves correctly when visitors move between pages without a full reload.
How do I keep HubSpot attribution when visitors come from ads?
Keep UTM parameters intact through any redirects, make sure the tracking code loads on the landing page, and submit forms with the hubspotutk cookie. HubSpot then connects the first visit's source to the contact when they convert. Test it with a tagged URL in a private window, then check the contact's original source in HubSpot.
Should the blog stay in HubSpot when I move to Next.js?
It can, through HubSpot's CMS API, but you'd keep paying for HubSpot's website tools and maintain two content systems. Most teams move the blog into the same headless CMS as the rest of the site, so editors have one place to work. Keep the same slugs and redirect any URLs that change.

· CEO & Design Director

Copywriter, designer and developer, and founder of Designify Studio. Over five years, 60+ projects across 14+ industries, for startups that have raised $300M+.

Want this done for you? Website, $8,000.

A multi-page marketing site, designed by us, developed in Next.js and animated. $8,000, one-time, up to 6 pages. Book a free 30-minute call to see if it fits.

Schedule Call