Skip to content
This repository has been archived by the owner on Nov 6, 2023. It is now read-only.

Commit

Permalink
feat: support Hydrogen 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Jun 22, 2022
1 parent 873036e commit 008714f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 37 deletions.
20 changes: 6 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Hydrogen on Netlify Edge Functions

This package is a Hydrogen platform that allows you to deploy your site to Netlify Edge. The easiest way to get started is using a template, but you can change your existing site to use Netlify Edge Functions by installing this package and following these instructions.
This package is a Hydrogen platform that allows you to deploy your site to Netlify Edge Functions.

## Installation

Expand All @@ -13,27 +13,19 @@ Then add the plugin to your Vite config:
```js
// vite.config.js

import { defineConfig } from 'vite'
import hydrogen from '@shopify/hydrogen/plugin'
import netlifyPlugin from '@netlify/hydrogen-platform/plugin'
import shopifyConfig from './shopify.config'

export default defineConfig({
plugins: [hydrogen(shopifyConfig), netlifyPlugin()],
plugins: [hydrogen(), netlifyPlugin()],
// ...
})
```

You then need to specify the SSR entrypoint in your build command:
Then when you run `shopify hydrogen build` it will generate the correct files to deploy to Netlify Edge.

```json
// package.json
"scripts": {
"build" "npm run build:client && npm run build:ssr",
"build:client": "vite build --outDir dist/client --manifest",
"build:ssr": "cross-env WORKER=true vite build --ssr @netlify/hydrogen-platform/handler",
}
```

Your Netlify site should be configured to publish the `dist/client` directory:
Netlify detects Hydrogen sites, so you should have the correct settings already, but if you need to set them manually you can use the `netlify.toml` or Netlify UI to do so:

```toml
# netlify.toml
Expand Down
12 changes: 5 additions & 7 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@ const handler = async (request: Request, context: any) => {
return
}

// Badly-behaved libraries look for window instead of document to
// check if it's in a browser.
delete (globalThis as any).window
;(globalThis as any).Oxygen ||= {}
// @ts-ignore Deno global is available at runtime
;(globalThis as any).Oxygen.env = Deno.env.toObject()

// This is where Hydrogen looks for the env var
;(globalThis as any).Oxygen ||= { env: {} }
;(globalThis as any).Oxygen.env.HYDROGEN_ENABLE_WORKER_STREAMING = true
// IP is on the context object. By default, this is where Hydrogen looks for it
request.headers.set('x-forwarded-for', context.ip)

try {
return await handleRequest(request, {
indexTemplate,
context,
buyerIpHeader: 'x-nf-client-connection-ip',
})
} catch (error: any) {
return new Response(error.message || error.toString(), { status: 500 })
Expand Down
33 changes: 17 additions & 16 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,34 @@ import MagicString from 'magic-string'
import type { Plugin, ResolvedConfig } from 'vite'

const HYDROGEN_DEFAULT_SERVER_ENTRY = '/src/App.server'
const PLATFORM_MODULE = '@netlify/hydrogen-platform/handler'

const plugin = (): Array<Plugin> => {
let resolvedConfig: ResolvedConfig
let platformEntryPath: string
const platformEntrypoint = require.resolve(
'@netlify/hydrogen-platform/handler'
)
return [
netlifyPlugin(),
{
name: 'vite-plugin-netlify-hydrogen',
config(config) {
// If we're building for SSR, ensure we use the Netlify entrypoint and output directory
if (config?.build?.ssr) {
return {
build: {
ssr: platformEntrypoint,
outDir: '.netlify/edge-functions/handler',
},
}
}
},
configResolved(config) {
// Save the config for later use
resolvedConfig = config
},
resolveId(id, importer) {
if (normalizePath(id).endsWith(PLATFORM_MODULE)) {
const platformPath = path.dirname(
require.resolve('@netlify/hydrogen-platform/package.json')
)
platformEntryPath = path.resolve(platformPath, 'dist', 'handler.mjs')

return this.resolve(platformEntryPath, importer, {
skipSelf: true,
})
}
return null
},
transform(code, id) {
if (normalizePath(id).endsWith(platformEntryPath)) {
// Replace the magic strings in the server entrypoint with the correct values
if (normalizePath(id) === platformEntrypoint) {
code = code
.replace(
'__SERVER_ENTRY__',
Expand Down

0 comments on commit 008714f

Please sign in to comment.