From bb5907d21f3200a51772a2cc2da8f2b7a85b7952 Mon Sep 17 00:00:00 2001 From: Pavel Mineev Date: Thu, 7 Jan 2021 22:00:27 +0300 Subject: [PATCH] Replacing REMARK_URL, fix remark_config stub, add minify html/css in templates --- Dockerfile.artifacts | 3 +- docker-init.sh | 2 +- frontend/app/__stubs__/remark-config.ts | 4 + frontend/app/common/constants.config.ts | 2 +- frontend/app/common/fetcher.ts | 7 +- .../app/components/list-comments/index.ts | 1 - .../list-comments/list-comments.tsx | 3 +- frontend/app/last-comments.tsx | 14 +- frontend/jest.config.js | 1 + frontend/package.json | 6 +- frontend/templates/comments.ejs | 84 ++++++++ frontend/templates/deleteme.ejs | 3 +- frontend/tsconfig.json | 1 + frontend/webpack.config.js | 197 ++++++++++-------- 14 files changed, 222 insertions(+), 106 deletions(-) create mode 100644 frontend/app/__stubs__/remark-config.ts create mode 100644 frontend/templates/comments.ejs diff --git a/Dockerfile.artifacts b/Dockerfile.artifacts index 80308ec3..1fc5945d 100644 --- a/Dockerfile.artifacts +++ b/Dockerfile.artifacts @@ -42,7 +42,8 @@ COPY --from=build-frontend /srv/frontend/public/ web RUN \ export WEB_ROOT=/build/backend/web && \ - sed -i "s|https://demo.remark42.com|http://127.0.0.1:8080|g" ${WEB_ROOT}/*.js && \ + sed -i "s|{% REMARK_URL %}http://127.0.0.1:8080|g" ${WEB_ROOT}/*.js && \ + sed -i "s|{% REMARK_URL %}http://127.0.0.1:8080|g" ${WEB_ROOT}/*.html && \ statik --src=${WEB_ROOT} --dest=/build/backend/app/rest -p api -f && \ statik --src=/build/backend/templates --dest=/build/backend/app -p templates -ns templates -f && \ ls -la /build/backend/app/templates/statik.go && \ diff --git a/docker-init.sh b/docker-init.sh index a329c67f..9aad5b4d 100755 --- a/docker-init.sh +++ b/docker-init.sh @@ -1,6 +1,6 @@ #!/bin/sh echo "prepare environment" -# replace BASE_URL constant by REMARK_URL +# replace {% REMARK_URL %} by content of REMARK_URL variable sed -i "s|{% REMARK_URL %}|${REMARK_URL}|g" /srv/web/*.html sed -i "s|{% REMARK_URL %}|${REMARK_URL}|g" /srv/web/*.js diff --git a/frontend/app/__stubs__/remark-config.ts b/frontend/app/__stubs__/remark-config.ts new file mode 100644 index 00000000..28545abf --- /dev/null +++ b/frontend/app/__stubs__/remark-config.ts @@ -0,0 +1,4 @@ +window.remark_config = { + host: 'http://test.com', + site_id: 'remark', +}; diff --git a/frontend/app/common/constants.config.ts b/frontend/app/common/constants.config.ts index 6bdbac53..2da61903 100644 --- a/frontend/app/common/constants.config.ts +++ b/frontend/app/common/constants.config.ts @@ -1,4 +1,4 @@ -export const BASE_URL = (window.remark_config && window.remark_config.host) || process.env.REMARK_URL!; +export const BASE_URL = window.remark_config.host; export const NODE_ID = process.env.REMARK_NODE!; export const API_BASE = '/api/v1'; export const COMMENT_NODE_CLASSNAME_PREFIX = 'remark42__comment-'; diff --git a/frontend/app/common/fetcher.ts b/frontend/app/common/fetcher.ts index 45a8194b..9b76992c 100644 --- a/frontend/app/common/fetcher.ts +++ b/frontend/app/common/fetcher.ts @@ -43,7 +43,7 @@ const fetcher = methods.reduce>((acc, method) => { contentType = 'application/json', logError = true, } = typeof data === 'string' ? { url: data } : data; - const basename = `${BASE_URL}${overriddenApiBase}`; + const baseUrl = `${BASE_URL}${overriddenApiBase}`; const headers = new Headers({ Accept: 'application/json', @@ -54,11 +54,12 @@ const fetcher = methods.reduce>((acc, method) => { headers.append('Content-Type', contentType); } + // Save token in memory and pass it into headers in case if storing cookies is disabled if (activeJwtToken) { headers.append(HEADER_X_JWT, activeJwtToken); } - let rurl = `${basename}${url}`; + let rurl = `${baseUrl}${url}`; const parameters: RequestInit = { method, @@ -118,7 +119,7 @@ const fetcher = methods.reduce>((acc, method) => { }); } - if (res.headers.has('Content-Type') && res.headers.get('Content-Type')!.indexOf('application/json') === 0) { + if (res.headers.get('Content-Type')?.startsWith('application/json')) { return res.json(); } diff --git a/frontend/app/components/list-comments/index.ts b/frontend/app/components/list-comments/index.ts index 9293c61d..08d6578b 100644 --- a/frontend/app/components/list-comments/index.ts +++ b/frontend/app/components/list-comments/index.ts @@ -1,4 +1,3 @@ -import './list-comments.css'; import './__item/list-comments__item.css'; export { default } from './list-comments'; diff --git a/frontend/app/components/list-comments/list-comments.tsx b/frontend/app/components/list-comments/list-comments.tsx index c40de0fc..96c000a9 100644 --- a/frontend/app/components/list-comments/list-comments.tsx +++ b/frontend/app/components/list-comments/list-comments.tsx @@ -1,5 +1,6 @@ import { h, FunctionComponent } from 'preact'; import { useIntl } from 'react-intl'; +import classnames from 'classnames'; import type { Comment as CommentType } from 'common/types'; import Comment from 'components/comment'; @@ -14,7 +15,7 @@ const ListComments: FunctionComponent = ({ comments = [] }) = const intl = useIntl(); return ( -
+
{comments.map(comment => ( { const nodes = document.getElementsByClassName(LAST_COMMENTS_NODE_CLASSNAME); if (!nodes) { - console.error("Remark42: Can't find last comments nodes."); - return; + throw new Error("Remark42: Can't find last comments nodes."); } - try { - window.remark_config = window.remark_config || {}; - } catch (e) { - console.error('Remark42: Config object is undefined.'); - return; + if (!window.remark_config) { + throw new Error('Remark42: Config object is undefined'); } const { site_id, max_last_comments } = window.remark_config; if (!site_id) { - console.error('Remark42: Site ID is undefined.'); - return; + throw new Error('Remark42: Site ID is undefined.'); } (Array.from(nodes) as HTMLElement[]).forEach(node => { const max = (node.dataset.max && parseInt(node.dataset.max, 10)) || max_last_comments || DEFAULT_LAST_COMMENTS_MAX; const locale = getLocale(window.remark_config); + Promise.all([getLastComments(site_id, max), loadLocale(locale)]).then(([comments, messages]) => { try { render( diff --git a/frontend/jest.config.js b/frontend/jest.config.js index 76d1962a..f5ec72f3 100644 --- a/frontend/jest.config.js +++ b/frontend/jest.config.js @@ -19,6 +19,7 @@ module.exports = { setupFilesAfterEnv: [ 'jest-localstorage-mock', '/app/__mocks__/headers.ts', + '/app/__stubs__/remark-config.ts', '/app/__stubs__/static-config.ts', ], collectCoverageFrom: ['!**/__mocks__/**', '!**/__stubs__/**', '!app/locales/**'], diff --git a/frontend/package.json b/frontend/package.json index 46f87cf6..a764704f 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -5,12 +5,12 @@ "scripts": { "build": "webpack --mode production", "build:analyze": "webpack --mode production --analyze", - "start": "webpack serve --mode development", - "dev": "cross-env REMARK_URL=http://127.0.0.1:8080 run-s npm start", + "start": "cross-env REMARK_API_BASE_URL=https://demo.remark42.com webpack serve --mode development", + "dev": "cross-env REMARK_URL=http://127.0.0.1:8080 webpack serve --mode development", "lint": "run-p lint:*", "test": "jest", "test:coverage": "jest --coverage", - "check": "cross-env NODE_ENV=production npm run build && run-p check:*", + "check": "cross-env NODE_ENV=production run-s build && run-p check:*", "check:types": "tsc -p tsconfig.json --noEmit", "check:translation": "run-s translation:extract translation:check", "check:size": "size-limit", diff --git a/frontend/templates/comments.ejs b/frontend/templates/comments.ejs new file mode 100644 index 00000000..cfdbd8d3 --- /dev/null +++ b/frontend/templates/comments.ejs @@ -0,0 +1,84 @@ + + + + + + Remark42 demo + + + + + + + + + \ No newline at end of file diff --git a/frontend/templates/deleteme.ejs b/frontend/templates/deleteme.ejs index 22cdbe36..75465cd6 100644 --- a/frontend/templates/deleteme.ejs +++ b/frontend/templates/deleteme.ejs @@ -6,6 +6,7 @@ Delete Me