Skip to content

Commit

Permalink
fix according to review
Browse files Browse the repository at this point in the history
  • Loading branch information
michenly committed Jun 11, 2024
1 parent 45aeac9 commit a3aa377
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 40 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ node_modules
.vscode
.turbo
.cache
.shopify
**/.shopify
/test-results/
/playwright-report/
/playwright/.cache/
Expand Down Expand Up @@ -139,4 +139,4 @@ yarn.lock
admin.schema.json
business-platform.schema.json

examples/**/*.generated.d.ts
examples/**/*.generated.d.ts
7 changes: 0 additions & 7 deletions examples/express/.gitignore

This file was deleted.

44 changes: 13 additions & 31 deletions examples/express/server.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import path from 'path';

import {createRequestHandler} from '@remix-run/express';
import {installGlobals, createCookieSessionStorage} from '@remix-run/node';
import compression from 'compression';
import express from 'express';
import morgan from 'morgan';
import {createStorefrontClient, InMemoryCache} from '@shopify/hydrogen';
import crypto from 'node:crypto';

installGlobals();

const env = process.env;

const BUILD_DIR = path.join(process.cwd(), 'build', 'index.js');

const vite =
process.env.NODE_ENV === 'production'
? undefined
Expand All @@ -31,30 +28,26 @@ app.use(compression());
// http://expressjs.com/en/advanced/best-practice-security.html#at-a-minimum-disable-x-powered-by-header
app.disable('x-powered-by');

// Remix fingerprints its assets so we can cache forever.
app.use(
'/build',
express.static('public/build', {immutable: true, maxAge: '1y'}),
);

// Everything else (like favicon.ico) is cached for an hour. You may want to be
// more aggressive with this caching.
app.use(express.static('public', {maxAge: '1h'}));

// handle asset requests
if (vite) {
app.use(vite.middlewares);
} else {
// add morgan here for production only
// dev uses morgan plugin, otherwise it spams the console with HMR requests
app.use(morgan('tiny'));
app.use(
'/assets',
express.static('build/client/assets', {immutable: true, maxAge: '1y'}),
);
}
app.use(express.static('build/client', {maxAge: '1h'}));

app.all(
'*',
process.env.NODE_ENV === 'development'
? async (req, res, next) => {
const context = await getContext(req);

purgeRequireCache();

return createRequestHandler({
build: vite
? () => vite.ssrLoadModule('virtual:remix/server-build')
Expand All @@ -81,19 +74,6 @@ app.listen(port, () => {
console.log(`Express server listening on port ${port}`);
});

function purgeRequireCache() {
// purge require cache on requests for "server side HMR" this won't let
// you have in-memory objects between requests in development,
// alternatively you can set up nodemon/pm2-dev to restart the server on
// file changes, but then you'll have to reconnect to databases/etc on each
// change. We prefer the DX of this, so we've included it for you by default
for (const key in import.meta.cache) {
if (key.startsWith(BUILD_DIR)) {
delete import.meta.cache[key];
}
}
}

async function getContext(req) {
const session = await AppSession.init(req, [env.SESSION_SECRET]);

Expand All @@ -109,8 +89,10 @@ async function getContext(req) {
storeDomain: env.PUBLIC_STORE_DOMAIN,
storefrontId: env.PUBLIC_STOREFRONT_ID,
storefrontHeaders: {
requestGroupId: req.get('request-id'),
buyerIp: req.get('oxygen-buyer-ip'),
requestGroupId: crypto.randomUUID(),
buyerIp: (req.headers['x-forwarded-for'] || req.connection.remoteAddress)
.split(':')
.pop(),
cookie: req.get('cookie'),
},
});
Expand Down

0 comments on commit a3aa377

Please sign in to comment.