Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨[RUMF-264] add compatibility with server side rendering #273

Merged
merged 5 commits into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ build:
script:
- yarn
- yarn build
- yarn test:tsc:3.0

compatibility:
stage: test
tags: ['runner:main', 'size:large']
image: $CI_IMAGE
script:
- yarn
- yarn test:compat:tsc
- yarn test:compat:ssr

unit:
stage: test
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dev": "ENV=development node test/server/server.js",
"release": "lerna version --exact",
"publish:npm": "TARGET_DATACENTER=us TARGET_ENV=production VERSION=release yarn build && lerna publish from-package",
"fail": "./scripts/fail.sh",
"test": "yarn test:unit",
"test:unit": "karma start ./test/unit/karma.local.conf.js",
"test:unit:cbt": "node ./scripts/cbt-with-retry.js karma start test/unit/karma.cbt.conf.js",
Expand All @@ -21,7 +22,8 @@
"test:e2e:npm": "TARGET_ENV=e2e-test yarn build && (cd test/app && rm -rf node_modules && yarn && yarn build) && E2E_MODE=npm wdio test/e2e/wdio.local.conf.js",
"test:e2e:async": "TARGET_ENV=e2e-test yarn build && E2E_MODE=async wdio test/e2e/wdio.local.conf.js",
"test:e2e:cbt": "TARGET_ENV=e2e-test yarn build && node ./scripts/cbt-with-retry.js wdio test/e2e/wdio.cbt.conf.js",
"test:tsc:3.0": "yarn build && (cd test/app && rm -rf node_modules && yarn && yarn tsc:compatibility)"
"test:compat:tsc": "yarn build && (cd test/app && rm -rf node_modules && yarn && yarn compat:tsc) || yarn fail 'typescript 3.0 compatibility broken'",
"test:compat:ssr": "yarn build && (cd test/app && rm -rf node_modules && yarn && yarn compat:ssr) || yarn fail 'server side rendering compatibility broken'"
},
"devDependencies": {
"@types/jasmine": "3.3.13",
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,8 @@ export function objectValues(object: { [key: string]: unknown }) {
})
return values
}

export function getGlobalObject<T>(): T {
// tslint:disable-next-line: function-constructor no-function-constructor-with-string-args
return (typeof globalThis === 'object' ? globalThis : Function('return this')()) as T
}
3 changes: 2 additions & 1 deletion packages/logs/src/logs.entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
commonInit,
Context,
ContextValue,
getGlobalObject,
isPercentage,
makeGlobal,
makeStub,
Expand Down Expand Up @@ -117,4 +118,4 @@ interface BrowserWindow extends Window {
DD_LOGS?: LogsGlobal
}

;(window as BrowserWindow).DD_LOGS = datadogLogs
getGlobalObject<BrowserWindow>().DD_LOGS = datadogLogs
3 changes: 2 additions & 1 deletion packages/rum/src/rum.entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
commonInit,
Context,
ContextValue,
getGlobalObject,
isPercentage,
makeGlobal,
makeStub,
Expand Down Expand Up @@ -108,4 +109,4 @@ interface BrowserWindow extends Window {
DD_RUM?: RumGlobal
}

;(window as BrowserWindow).DD_RUM = datadogRum
getGlobalObject<BrowserWindow>().DD_RUM = datadogRum
7 changes: 7 additions & 0 deletions scripts/fail.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

echo
echo "❌ ${1}"
echo

exit 1
15 changes: 9 additions & 6 deletions test/app/app.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { datadogLogs } from '@datadog/browser-logs'
import { datadogRum } from '@datadog/browser-rum'

// fallback for server side rendering
const origin = typeof location === 'object' ? location.origin : ''

datadogLogs.init({
clientToken: 'key',
forwardErrorsToLogs: true,
internalMonitoringEndpoint: `${window.location.origin}/monitoring`,
logsEndpoint: `${window.location.origin}/logs`,
rumEndpoint: `${window.location.origin}/rum`,
internalMonitoringEndpoint: `${origin}/monitoring`,
logsEndpoint: `${origin}/logs`,
rumEndpoint: `${origin}/rum`,
})

datadogRum.init({
applicationId: 'rum',
clientToken: 'key',
internalMonitoringEndpoint: `${window.location.origin}/monitoring`,
logsEndpoint: `${window.location.origin}/logs`,
rumEndpoint: `${window.location.origin}/rum`,
internalMonitoringEndpoint: `${origin}/monitoring`,
logsEndpoint: `${origin}/logs`,
rumEndpoint: `${origin}/rum`,
})
5 changes: 3 additions & 2 deletions test/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"name": "app",
"version": "0.0.0",
"scripts": {
"tsc:compatibility": "tsc -p tsconfig.json",
"build": "webpack --mode=production"
"build": "webpack --mode=production",
"compat:tsc": "tsc -p tsconfig.json",
"compat:ssr": "webpack --mode=development && node dist/app.js"
},
"dependencies": {
"@datadog/browser-logs": "file:../../packages/logs",
Expand Down
2 changes: 1 addition & 1 deletion test/app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"outDir": "./dist/",
"strict": true,
"module": "es6",
"module": "commonjs",
"moduleResolution": "node",
"target": "es5"
}
Expand Down
8 changes: 6 additions & 2 deletions test/app/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path')

module.exports = {
module.exports = (env, argv) => ({
entry: './app.ts',
module: {
rules: [
Expand All @@ -13,8 +13,12 @@ module.exports = {
resolve: {
extensions: ['.ts', '.js'],
},
optimization: {
// Display stack trace when SSR test fail
minimize: argv.mode === 'development',
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'app.js',
},
}
})
12 changes: 6 additions & 6 deletions test/app/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
# yarn lockfile v1


"@datadog/browser-core@1.2.7", "@datadog/browser-core@file:../../packages/core":
version "1.2.7"
"@datadog/browser-core@1.4.1", "@datadog/browser-core@file:../../packages/core":
version "1.4.1"
dependencies:
lodash.assign "4.2.0"
lodash.merge "4.6.2"
tslib "1.10.0"
url-polyfill "1.1.7"

"@datadog/browser-logs@file:../../packages/logs":
version "1.2.7"
version "1.4.1"
dependencies:
"@datadog/browser-core" "1.2.7"
"@datadog/browser-core" "1.4.1"
lodash.assign "4.2.0"
lodash.merge "4.6.2"
tslib "1.10.0"

"@datadog/browser-rum@file:../../packages/rum":
version "1.2.7"
version "1.4.1"
dependencies:
"@datadog/browser-core" "1.2.7"
"@datadog/browser-core" "1.4.1"
lodash.assign "4.2.0"
lodash.merge "4.6.2"
tslib "1.10.0"
Expand Down
2 changes: 0 additions & 2 deletions test/e2e/wdio.base.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ module.exports = {
files: true,
project: 'test/e2e/scenario/tsconfig.json',
})
// avoid reference error when executing ts files with window references
global.window = {}
},
onComplete: function() {
serverProcess.kill()
Expand Down