|
| 1 | +--- |
| 2 | +title: Self-hosting |
| 3 | +description: Learn how to self-host your Nordcraft website on your own server, Docker, or Cloudflare Workers using the open-source Nordcraft backend. |
| 4 | +--- |
| 5 | + |
| 6 | +# Self-hosting |
| 7 | + |
| 8 | +By default, Nordcraft hosts your websites on Nordcraft's infrastructure. Because Nordcraft is open source, you can also export your project and **self-host** it on your own infrastructure using the open-source backend package in the [Nordcraft GitHub repository](https://github.com/nordcraftengine/nordcraft/tree/main/packages/backend). |
| 9 | + |
| 10 | +::: info |
| 11 | +The Nordcraft self-hosting backend package is still under active development. Check the [Nordcraft repository on GitHub](https://github.com/nordcraftengine/nordcraft/tree/main/packages/backend) for the latest updates and improvements. |
| 12 | +::: |
| 13 | + |
| 14 | +## What is included |
| 15 | + |
| 16 | +When you self-host a Nordcraft website with the `@nordcraft/backend` package, your site runs with the core features you get on Nordcraft's hosting: |
| 17 | + |
| 18 | +- **Server-side rendering (SSR)**: Pages are rendered on the server for fast initial load times and search engine optimization (SEO), then hydrated in the browser for interactivity. |
| 19 | +- **Custom formulas and actions**: Your project's custom formulas and actions work during both server-side rendering and in the browser. |
| 20 | +- **Redirects and proxies**: Handles the [redirects](/pages/redirects) and [proxies](/pages/proxies) configured in your project. |
| 21 | +- **API requests and cookies**: Proxies client API requests and supports [HTTP-only cookies](/connecting-data/authentication) for authentication. |
| 22 | +- **Fonts and site files**: Serves Google Fonts, page stylesheets, exported [custom elements](/components/export-a-component), `/sitemap.xml`, `/robots.txt`, `/manifest.json`, and `/favicon.ico` based on your project configuration. |
| 23 | + |
| 24 | +## Before you start |
| 25 | + |
| 26 | +To self-host a Nordcraft website, you need: |
| 27 | + |
| 28 | +- A Nordcraft website project that you want to host |
| 29 | +- [Bun](https://bun.sh/) installed on your computer to install dependencies and prepare your project |
| 30 | +- A hosting provider if you plan to publish online, such as [Cloudflare Workers](https://workers.cloudflare.com/), [Docker](https://www.docker.com/), or a server running Bun or Node.js |
| 31 | + |
| 32 | +## Export and set up your project |
| 33 | + |
| 34 | +Follow these steps to export your website from Nordcraft and add it to the backend package: |
| 35 | + |
| 36 | +1. Open your project in Nordcraft and go to the [Project details](/get-started/project-details#branches) page. |
| 37 | +2. Find the branch you want to host (usually `main`), open its actions menu, and select **Download branch** to save your project as a JSON file. |
| 38 | +3. Clone the [Nordcraft repository](https://github.com/nordcraftengine/nordcraft) from GitHub, install the dependencies, and build the workspace packages from the root folder: |
| 39 | + |
| 40 | +```bash |
| 41 | +git clone https://github.com/nordcraftengine/nordcraft.git |
| 42 | +cd nordcraft |
| 43 | +bun install |
| 44 | +bun run build |
| 45 | +``` |
| 46 | + |
| 47 | +4. Move the JSON file you downloaded from Nordcraft into `packages/backend/__project__/project.json`, replacing the example `project.json` file. |
| 48 | + |
| 49 | +::: tip |
| 50 | +Your self-hosted site serves the snapshot you exported in `project.json`. Whenever you publish new changes in Nordcraft, download the updated branch, replace `project.json`, and redeploy. |
| 51 | +::: |
| 52 | + |
| 53 | +## Run locally |
| 54 | + |
| 55 | +Before deploying to a live server, you can test your self-hosted website locally. Navigate to the backend folder and start the development server: |
| 56 | + |
| 57 | +```bash |
| 58 | +cd packages/backend |
| 59 | +bun run dev |
| 60 | +``` |
| 61 | + |
| 62 | +This prepares your project's pages and static assets and starts a local server so you can preview your site in your browser. |
| 63 | + |
| 64 | +If you want to test the compiled production server locally, you can also run it directly with Bun (`bun run bunrun`) or Node.js (`bun run noderun`). |
| 65 | + |
| 66 | +## Deploy your website |
| 67 | + |
| 68 | +Once your project is set up in `packages/backend`, you can deploy it to Cloudflare Workers or package it with Docker to run on any server. |
| 69 | + |
| 70 | +### Deploy to Cloudflare Workers |
| 71 | + |
| 72 | +The backend package comes pre-configured for [Cloudflare Workers](https://workers.cloudflare.com/) in `packages/backend/wrangler.toml`. |
| 73 | + |
| 74 | +1. Provide your Cloudflare account ID by setting the `CLOUDFLARE_ACCOUNT_ID` environment variable or adding it to `packages/backend/wrangler.toml`. |
| 75 | +2. Deploy your website from the `packages/backend` folder: |
| 76 | + |
| 77 | +```bash |
| 78 | +bun run deploy |
| 79 | +``` |
| 80 | + |
| 81 | +This command prepares your project's static assets, bundles the application, and publishes it to Cloudflare Workers. |
| 82 | + |
| 83 | +### Deploy with Docker |
| 84 | + |
| 85 | +If you want to host your website on your own server or any container platform, use the included `Dockerfile` to build and run a lightweight container. |
| 86 | + |
| 87 | +From the `packages/backend` folder, run: |
| 88 | + |
| 89 | +```bash |
| 90 | +bun run docker:build |
| 91 | +bun run docker:run |
| 92 | +``` |
| 93 | + |
| 94 | +By default, the container serves your website on port `3000`. |
| 95 | + |
| 96 | +## Best practices |
| 97 | + |
| 98 | +Keep the following in mind when running a self-hosted Nordcraft website: |
| 99 | + |
| 100 | +- **Use HTTPS in production**: Serve your public website over HTTPS so features like [HTTP-only cookies](/connecting-data/authentication) work properly in browsers. |
| 101 | +- **Keep secrets out of your project**: Never store private API keys or sensitive credentials in your Nordcraft project. Read the [Security guide](/guides/security) to learn how to handle sensitive data safely. |
| 102 | +- **Keep your export in sync**: Self-hosted websites do not automatically update when you edit your project in Nordcraft. Re-export your branch and redeploy whenever you want changes to go live. |
0 commit comments