Skip to content

Commit

Permalink
fix ENVs in the server code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kallenju committed Dec 22, 2022
1 parent 9dfeee6 commit a08f465
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 0 additions & 2 deletions config/webpack.client.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ const basePlugins = [
processOutput(assets) {
const output = JSON.stringify(assets);

process.env.ASSETS_MAP_GENERAL = output;

return output;
},
}),
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "template-for-react-ts-ssr-hmr",
"version": "2.0.1",
"description": "Template for a project with React, TypeScript, SSR, HMR. The template implies the use of CSS Modules and Stylus.",
"version": "2.0.2",
"description": "Template for a project with React, TypeScript, Server-Side Rendering (SSR), Hot Module Replacing (HMR). The template implies the use of CSS Modules and Stylus.",
"repository": {
"type": "git",
"url": "https://github.com/Kallenju/template-for-react-ts-ssr-hmr.git"
Expand Down
11 changes: 9 additions & 2 deletions src/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,25 @@ dotenv.config({
path: path.resolve(__dirname, `../.env.${process.env.DOTENV}`),
});

const { SSR_ABORT_DELAY = 10000, ASSETS_MAP_GENERAL } = process.env;
const { NODE_ENV = 'production', SSR_ABORT_DELAY = 10000 } = process.env;

import fs from 'fs';
import { Response } from 'express';
import React, { StrictMode } from 'react';
import { renderToPipeableStream } from 'react-dom/server';
import AssetsMap from '../shared/interfaces/AssetsMap';
import App from '../App';

const isCrawler = false;
const pathToAssetsMap = path.resolve(
__dirname,
NODE_ENV === 'production' ? '../assetsMap.json' : '../../dist/assetsMap.json'
);

export default function render(url: string, response: Response): void {
const __ASSETS_MAP: AssetsMap = JSON.parse(ASSETS_MAP_GENERAL).main;
const __ASSETS_MAP: AssetsMap = JSON.parse(
fs.readFileSync(pathToAssetsMap, 'utf8')
).main;

let didError = false;
const stream: ReturnType<typeof renderToPipeableStream> =
Expand Down
14 changes: 13 additions & 1 deletion src/server/server.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
import path from 'path';
import url from 'url';
import dotenv from 'dotenv';

const __dirname: string = path.dirname(url.fileURLToPath(import.meta.url));

dotenv.config({
path: path.resolve(__dirname, `../.env.${process.env.DOTENV}`),
});

const { DEV_SERVER_PORT = 3000 } = process.env;

import express, { Request, Response } from 'express';
import compress from 'compression';
import render from './render';
Expand All @@ -11,4 +23,4 @@ app.get('/', (request: Request, response: Response) =>

app.use('/static', express.static('./dist/client'));

app.listen(3000);
app.listen(Number(DEV_SERVER_PORT));

0 comments on commit a08f465

Please sign in to comment.