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

Support SSR (Reopen #9317) #9385

Merged
merged 9 commits into from
Jul 6, 2024
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
28 changes: 28 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,34 @@ jobs:
env:
CI_COMMIT_SHA: ${{ github.event.pull_request.head.sha || github.sha }}

test-ssr:
needs: setup
runs-on: ubuntu-latest

steps:
jonkoops marked this conversation as resolved.
Show resolved Hide resolved
- name: Restore setup
uses: actions/cache@v4
with:
path: ./*
key: ${{ runner.os }}-${{ github.ref }}-${{ github.sha }}-setup

- name: Set up Node
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: npm

- name: Run Node.js SSR script
run: node ./spec/ssr/ssr_node.mjs

- name: Set up Deno
uses: denoland/setup-deno@v1
with:
deno-version: v1.x

- name: Run Deno SSR script
run: deno run ./spec/ssr/ssr_deno.js

test:
needs: setup
runs-on: ${{ matrix.os || 'ubuntu-latest' }}
Expand Down
2 changes: 2 additions & 0 deletions spec/ssr/ssr_deno.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import L from '../../dist/leaflet-src.esm.js';
console.log(L.version);
2 changes: 2 additions & 0 deletions spec/ssr/ssr_node.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import L from '../../dist/leaflet-src.js';
jonkoops marked this conversation as resolved.
Show resolved Hide resolved
console.log(L.version);
14 changes: 8 additions & 6 deletions src/core/Browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ const mobile = typeof orientation !== 'undefined' || userAgentContains('mobile')

// @property pointer: Boolean
// `true` for all browsers supporting [pointer events](https://msdn.microsoft.com/en-us/library/dn433244%28v=vs.85%29.aspx).
const pointer = !!window.PointerEvent;
const pointer = typeof window === 'undefined' ? false : !!window.PointerEvent;

// @property touchNative: Boolean
// `true` for all browsers supporting [touch events](https://developer.mozilla.org/docs/Web/API/Touch_events).
// **This does not necessarily mean** that the browser is running in a computer with
// a touchscreen, it only means that the browser is capable of understanding
// touch events.
const touchNative = 'ontouchstart' in window || !!window.TouchEvent;
const touchNative = typeof window === 'undefined' ? false : 'ontouchstart' in window || !!(window.TouchEvent);

// @property touch: Boolean
// `true` for all browsers supporting either [touch](#browser-touch) or [pointer](#browser-pointer) events.
Expand All @@ -40,19 +40,21 @@ const touch = touchNative || pointer;

// @property retina: Boolean
// `true` for browsers on a high-resolution "retina" screen or on any screen when browser's display zoom is more than 100%.
const retina = (window.devicePixelRatio || (window.screen.deviceXDPI / window.screen.logicalXDPI)) > 1;
const retina = typeof window === 'undefined' || typeof window.devicePixelRatio === 'undefined' ? false : window.devicePixelRatio > 1;

// @property mac: Boolean; `true` when the browser is running in a Mac platform
const mac = navigator.platform.startsWith('Mac');
const mac = typeof navigator === 'undefined' || typeof navigator.platform === 'undefined' ? false : navigator.platform.startsWith('Mac');

// @property mac: Boolean; `true` when the browser is running in a Linux platform
const linux = navigator.platform.startsWith('Linux');
const linux = typeof navigator === 'undefined' || typeof navigator.platform === 'undefined' ? false : navigator.platform.startsWith('Linux');
jonkoops marked this conversation as resolved.
Show resolved Hide resolved

function userAgentContains(str) {
if (typeof navigator === 'undefined' || typeof navigator.userAgent === 'undefined') {
return false;
}
return navigator.userAgent.toLowerCase().includes(str);
}


export default {
chrome,
safari,
Expand Down
4 changes: 2 additions & 2 deletions src/core/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ export function template(str, data) {
// mobile devices (by setting image `src` to this string).
export const emptyImageUrl = 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=';

const requestFn = window.requestAnimationFrame;
const cancelFn = window.cancelAnimationFrame;
const requestFn = typeof window === 'undefined' ? falseFn : window.requestAnimationFrame;
const cancelFn = typeof window === 'undefined' ? falseFn : window.cancelAnimationFrame;

// @function requestAnimFrame(fn: Function, context?: Object): Number
// Schedules `fn` to be executed when the browser repaints. `fn` is bound to `context` if given.
Expand Down
2 changes: 1 addition & 1 deletion src/dom/DomEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function batchRemove(obj, filterFn) {
const mouseSubst = {
mouseenter: 'mouseover',
mouseleave: 'mouseout',
wheel: !('onwheel' in window) && 'mousewheel'
wheel: typeof window === 'undefined' ? false : !('onwheel' in window) && 'mousewheel'
};

function addOne(obj, type, fn, context) {
Expand Down
2 changes: 1 addition & 1 deletion src/dom/DomUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function getPosition(el) {
return positions.get(el) ?? new Point(0, 0);
}

const documentStyle = document.documentElement.style;
const documentStyle = typeof document === 'undefined' ? {} : document.documentElement.style;
// Safari still needs a vendor prefix, we need to detect with property name is supported.
const userSelectProp = ['userSelect', 'WebkitUserSelect'].find(prop => prop in documentStyle);
let prevUserSelect;
Expand Down