diff --git a/client/package.json b/client/package.json index d6e5e5c5..c30b8d48 100644 --- a/client/package.json +++ b/client/package.json @@ -2,7 +2,7 @@ "name": "client", "version": "0.1.0", "private": true, - "homepage": ".", + "homepage": "/frontend", "dependencies": { "@testing-library/jest-dom": "^4.2.4", "@testing-library/react": "^9.3.2", diff --git a/cms/middlewares/frontend/defaults.json b/cms/middlewares/frontend/defaults.json new file mode 100644 index 00000000..7d452152 --- /dev/null +++ b/cms/middlewares/frontend/defaults.json @@ -0,0 +1,13 @@ +{ + "frontend": { + "enabled": true, + "path": "./public/frontend", + "routes": [ + "/", + "/login", + "/register", + "/teacher*", + "/workspace*" + ] + } +} \ No newline at end of file diff --git a/cms/middlewares/frontend/index.js b/cms/middlewares/frontend/index.js new file mode 100644 index 00000000..5015d9f9 --- /dev/null +++ b/cms/middlewares/frontend/index.js @@ -0,0 +1,30 @@ +'use strict' + +/** + * Module dependencies + */ +const fs = require('fs') +const path = require('path') + +module.exports = strapi => { + return { + /** + * Initialize the hook + */ + + async initialize() { + // Configuration from middlewares/frontend/defaults.json + const { path: frontendPath, routes } = strapi.config.middleware.settings.frontend + const frontendDir = path.resolve(strapi.dir, frontendPath) + + const serveFrontend = ctx => { + ctx.type = 'html' + ctx.body = fs.createReadStream(path.join(frontendDir + '/index.html')) + } + + // Serve the index.html for all client routes + // This allows the react router to work properly + routes.forEach(path => strapi.router.get(path, serveFrontend)) + }, + } +} \ No newline at end of file diff --git a/cms/package.json b/cms/package.json index 42993708..df5de6a2 100644 --- a/cms/package.json +++ b/cms/package.json @@ -10,7 +10,8 @@ "strapi": "strapi", "db-make": "docker run --name stem-c-db-dev -p 5432:5432 -e POSTGRES_DB=strapi -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -v /var/lib/postgresql/data -d postgres", "db-start": "docker start stem-c-db-dev", - "db-bash": "docker exec -it stem-c-db-dev psql strapi postgres" + "db-bash": "docker exec -it stem-c-db-dev psql strapi postgres", + "build-frontend": "rm -rf ./public/frontend/* && cd ../client && yarn build && mv ./build/* ../cms/public/frontend" }, "devDependencies": {}, "dependencies": {