Skip to content

Commit

Permalink
Merge pull request #18381 from apache/ssr
Browse files Browse the repository at this point in the history
feat(ssr): server-side rendering and client hydration #18334
  • Loading branch information
Ovilia committed Nov 16, 2023
2 parents fd9e62d + badfd0f commit b07ce79
Show file tree
Hide file tree
Showing 15 changed files with 489 additions and 10 deletions.
1 change: 1 addition & 0 deletions .github/workflows/source-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ jobs:
index.d.ts
src/
extension-src/
ssr/client/src/
licenses/
theme/
build/
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ todo
/index.blank.js
/extension-esm
/extension
/ssr/client/lib
/core.js
/core.d.ts
/charts.js
Expand Down
6 changes: 6 additions & 0 deletions build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ async function run() {
];
await build(cfgs);
}
else if (buildType === 'ssr') {
const cfgs = [
config.createSSRClient(opt)
];
await build(cfgs);
}
else if (buildType === 'myTransform') {
const cfgs = [
config.createMyTransform(opt)
Expand Down
16 changes: 16 additions & 0 deletions build/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,19 @@ exports.createMyTransform = function (opt) {
)
};
};

exports.createSSRClient = function (opt) {
const input = nodePath.resolve(ecDir, `ssr/client/lib/index.js`);

return {
plugins: [nodeResolvePlugin()],
input: input,
output: createOutputs(
nodePath.resolve(ecDir, `ssr/client/dist/index`),
opt,
{
name: 'echarts-ssr-client'
}
)
};
};
28 changes: 28 additions & 0 deletions build/pre-publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ const extensionSrcGlobby = {
};
const extensionSrcDir = nodePath.resolve(ecDir, 'extension-src');
const extensionESMDir = nodePath.resolve(ecDir, 'extension');
const ssrClientGlobby = {
patterns: [
'ssr/client/src/**/*.ts'
],
cwd: ecDir
};
const ssrClientSrcDir = nodePath.resolve(ecDir, 'ssr/client/src');
const ssrClientESMDir = nodePath.resolve(ecDir, 'ssr/client/lib');

const typesDir = nodePath.resolve(ecDir, 'types');
const esmDir = 'lib';
Expand Down Expand Up @@ -135,6 +143,26 @@ const compileWorkList = [
after: async function () {
await transformLibFiles(extensionESMDir, 'lib');
}
},
{
logLabel: 'ssr client ts -> js-esm',
compilerOptionsOverride: {
module: 'ES2015',
declaration: false,
rootDir: ssrClientSrcDir,
outDir: ssrClientESMDir
},
srcGlobby: ssrClientGlobby,
transformOptions: {
filesGlobby: {patterns: ['**/*.js'], cwd: ssrClientESMDir},
transformDEV: true
},
before: async function () {
fsExtra.removeSync(ssrClientESMDir);
},
after: async function () {
await transformLibFiles(ssrClientESMDir, 'lib');
}
}
];

Expand Down
15 changes: 8 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@
"build:i18n": "node build/build-i18n.js",
"build:lib": "node build/build.js --prepublish",
"build:extension": "node build/build.js --type extension",
"build:ssr": "node build/build.js --type ssr",
"dev:fast": "node build/build-i18n.js && node build/dev-fast.js",
"dev": "npx -y concurrently -n build,server \"npm run dev:fast\" \"npx -y http-server -c-1 -s -o test\"",
"prepare": "npm run build:lib && husky install",
"release": "npm run build:lib && npm run build:i18n && npm run build && npm run build:esm && npm run build:extension",
"release": "npm run build:lib && npm run build:i18n && npm run build && npm run build:esm && npm run build:extension && npm run build:ssr",
"help": "node build/build.js --help",
"test:visual": "node test/runTest/server.js",
"test": "npx jest --config test/ut/jest.config.js",
Expand All @@ -64,7 +65,7 @@
},
"dependencies": {
"tslib": "2.3.0",
"zrender": "5.4.4"
"zrender": "npm:zrender-nightly@^5.4.4-dev.20231116"
},
"devDependencies": {
"@babel/code-frame": "7.10.4",
Expand Down
20 changes: 19 additions & 1 deletion src/component/legend/LegendView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {LineStyleProps} from '../../model/mixin/lineStyle';
import {createSymbol, ECSymbol} from '../../util/symbol';
import SeriesModel from '../../model/Series';
import { createOrUpdatePatternFromDecal } from '../../util/decal';
import { getECData } from '../../util/innerStore';

const curry = zrUtil.curry;
const each = zrUtil.each;
Expand Down Expand Up @@ -225,6 +226,13 @@ class LegendView extends ComponentView {
.on('mouseover', curry(dispatchHighlightAction, seriesModel.name, null, api, excludeSeriesId))
.on('mouseout', curry(dispatchDownplayAction, seriesModel.name, null, api, excludeSeriesId));

itemGroup.eachChild(child => {
const ecData = getECData(child);
ecData.seriesIndex = seriesModel.seriesIndex;
ecData.dataIndex = dataIndex;
ecData.ssrType = 'legend';
});

legendDrawnMap.set(name, true);
}
else {
Expand Down Expand Up @@ -269,6 +277,13 @@ class LegendView extends ComponentView {
.on('mouseover', curry(dispatchHighlightAction, null, name, api, excludeSeriesId))
.on('mouseout', curry(dispatchDownplayAction, null, name, api, excludeSeriesId));

itemGroup.eachChild(child => {
const ecData = getECData(child);
ecData.seriesIndex = seriesModel.seriesIndex;
ecData.dataIndex = dataIndex;
ecData.ssrType = 'legend';
});

legendDrawnMap.set(name, true);
}

Expand Down Expand Up @@ -430,7 +445,10 @@ class LegendView extends ComponentView {
// Add a invisible rect to increase the area of mouse hover
const hitRect = new graphic.Rect({
shape: itemGroup.getBoundingRect(),
invisible: true
style: {
// Cannot use 'invisible' because SVG SSR will miss the node
fill: 'transparent'
}
});

const tooltipModel =
Expand Down
9 changes: 9 additions & 0 deletions src/core/echarts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,15 @@ class ECharts extends Eventful<ECEventDefinition> {

}

zrender.registerSSRDataGetter(el => {
const ecData = getECData(el);
const hashMap = createHashMap();
hashMap.set('series_index', ecData.seriesIndex);
hashMap.set('data_index', ecData.dataIndex);
hashMap.set('ssr_type', ecData.ssrType);
return hashMap;
});

const zr = this._zr = zrender.init(dom, {
renderer: opts.renderer || defaultRenderer,
devicePixelRatio: opts.devicePixelRatio,
Expand Down
6 changes: 6 additions & 0 deletions src/util/innerStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import {
ComponentMainType, ComponentItemTooltipOption
} from './types';
import { makeInner } from './model';

export type SSRItemType = 'chart' | 'legend';

/**
* ECData stored on graphic element
*/
Expand All @@ -34,6 +37,7 @@ export interface ECData {
dataType?: SeriesDataType;
focus?: InnerFocus;
blurScope?: BlurScope;
ssrType?: SSRItemType;

// Required by `tooltipConfig` and `focus`.
componentMainType?: ComponentMainType;
Expand Down Expand Up @@ -62,6 +66,7 @@ export const setCommonECData = (seriesIndex: number, dataType: SeriesDataType, d
ecData.dataIndex = dataIdx;
ecData.dataType = dataType;
ecData.seriesIndex = seriesIndex;
ecData.ssrType = 'chart';

// TODO: not store dataIndex on children.
if (el.type === 'group') {
Expand All @@ -70,6 +75,7 @@ export const setCommonECData = (seriesIndex: number, dataType: SeriesDataType, d
childECData.seriesIndex = seriesIndex;
childECData.dataIndex = dataIdx;
childECData.dataType = dataType;
childECData.ssrType === 'chart';
});
}
}
Expand Down
109 changes: 109 additions & 0 deletions ssr/client/dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ssr/client/dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b07ce79

Please sign in to comment.