Open Wombosvideo opened 1 year ago
Same issue, not working with Sveltekit.
Error thrown:
ReferenceError: navigator is not defined at DeviceIsMobile (file:///Users/hansgruber/Desktop/webdev/projects/dundermifflin-ui/node_modules/svelte-device-info/dist/svelte-device-info.esm.js:10:27) at get isMobile [as isMobile] (file:///Users/hansgruber/Desktop/webdev/projects/dundermifflin-ui/node_modules/svelte-device-info/dist/svelte-device-info.esm.js:236:29) at /Users/hansgruber/Desktop/webdev/projects/dundermifflin-ui/src/routes/pos/+page.svelte:115:45 at Object.$$render (/Users/hansgruber/Desktop/webdev/projects/dundermifflin-ui/node_modules/svelte/src/runtime/internal/ssr.js:156:16) at Object.default (/Users/hansgruber/Desktop/webdev/projects/dundermifflin-ui/.svelte-kit/generated/root.svelte:44:41) at eval (/Users/hansgruber/Desktop/webdev/projects/dundermifflin-ui/src/routes/+layout.svelte:59:270) at Object.$$render (/Users/hansgruber/Desktop/webdev/projects/dundermifflin-ui/node_modules/svelte/src/runtime/internal/ssr.js:156:16) at /Users/hansgruber/Desktop/webdev/projects/dundermifflin-ui/.svelte-kit/generated/root.svelte:43:40 at $$render (/Users/hansgruber/Desktop/webdev/projects/dundermifflin-ui/node_modules/svelte/src/runtime/internal/ssr.js:156:16) at Object.render (/Users/hansgruber/Desktop/webdev/projects/dundermifflin-ui/node_modules/svelte/src/runtime/internal/ssr.js:164:17
@Wombosvideo I tried your approach but doesn't seem to work. It does correctly detects it's not a mobile browsers when I access my web app from my laptop, but does not detect it correctly when I access it from my iPhone, it keeps saying "this device is not mobile" when printing:
console.log('this device is ' + (Device.isMobile ? '' : 'not') + ' mobile')
I no longer use this solution. I switched to a JavaScript based solution where the viewport width is used using a media query watcher. This way I can have the same solution to detect dark mode and mobile devices.
You're probably getting that because of SSR?
My workaround is doing this:
const isServer = typeof window === 'undefined';
let canHover = false;
if (!isServer) {
const { default: Device } = await import('svelte-device-info');
canHover = Device.canHover;
}
export { canHover };
Description
This module does not work with SvelteKit due to
navigator
not being present server-side.Log messages
Possible Solution
Create a
writable
store forisMobile
and update itonMount
.Sample Module
This is an example how the module could be updated:
This code also replaces the function syntax with an arrow function, replaces deprecated
substr(...)
withsubstring(...)
, and removes theResult
variable from the originalDeviceIsMobile()
function. It also implements correct typing and removes unnecessary\
escape characters in front of the-
hyphens.If you want to save this code as a module, store it in a
device-info.ts
file or similar.It could then be used like this: