S
SolidJS•13mo ago
Bersaelor

Pure client based page complaining about `window is not defined` in solid-start (No SSR needed)

Back when I was just working with solid.js I could use window in my code just fine, for. pages that are entirely client-rendered. With solid-start I use the solid-start-static for client based pages, like described in Alex' blog post https://dev.to/lexlohr/using-solid-start-with-github-pages-3iok .
export default defineConfig({
plugins: [solid({ adapter: staticAdapter() })],
});
export default defineConfig({
plugins: [solid({ adapter: staticAdapter() })],
});
But this way I still get Error when evaluating SSR module: window is not defined. How can I tell solid-start that I want a classic client-based SPA? (and no SSR needed)
20 Replies
Bersaelor
Bersaelor•13mo ago
(similar for other client based global variables like localStorage). Do I have to wrap all this in if (!isServer) even though I never do SSr in this project? Specifically, I use the aws-amplify module for authentication with AWS Cognito, and internally it uses a bunch of global ,window and localStorage
thetarnav
thetarnav•13mo ago
static adapter still does SSR, it's a normal SSR in dev and SSG at build to disable SSR you do { ssr: false } and then you don't need the static adapter, because you want only an empty .html file, not prerendered pages But imo it's better to leave ssr on, and just make sure the pages are rendered only on the client Then you can alvays opt-into SSR for some pages if you find the need, or at least SSR the app shell when youre separating isomorphic components from client-only, make sure to use Suspense + lazy to code-split and not load them on the server (tree-shaking doesn't work in dev) sth like this: https://github.com/thetarnav/mxyz-mark/blob/master/apps/solid-web/src/routes.tsx#L44
Bersaelor
Bersaelor•13mo ago
yup, it works at build time, I just wanted to see my code run with yarn dev. The offending code sits in root.tsx the auth-switch should happen before any route is accessible, therefore I guess I'll turn SSR off using { ssr: false } i.e. regardless which route you load, if not signed in I show the signin-ui I also have SSR rendered pages, but at the moment they sit in a different project on a different subdomain as the SPA mhmm, when I do:
export default defineConfig({
plugins: [solid({ ssr: false }), suidPlugin()],
});
export default defineConfig({
plugins: [solid({ ssr: false }), suidPlugin()],
});
I still get Uncaught ReferenceError: global is not defined when fetching the site after running yarn dev. Where do you put the ssr: false @thetarnav ?
thetarnav
thetarnav•13mo ago
what is global?
Bersaelor
Bersaelor•13mo ago
some global variable that browsers have that AWS amplify uses deep in its internals
thetarnav
thetarnav•13mo ago
well mine doesn't
Bersaelor
Bersaelor•13mo ago
I imported aws amplify the same way I used to in my other solid.js projects, where it just worked only difference is that now I'm using solid-start
thetarnav
thetarnav•13mo ago
I'm not sure is it still running on the server? if you log something will you see it logged by vite server?
Bersaelor
Bersaelor•13mo ago
ah, it might be a polyfill problem, since AWS imported buffer and global is a node thing https://github.com/aws/aws-sdk-js/issues/2141 it's an ongoing issue I remember now, you have to hack global into your index.html
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>

<script>
// aws-sdk requires global to exist
var global = global || window;
</script>
<script src="/src/index.tsx" type="module"></script>
</body>
</html>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>

<script>
// aws-sdk requires global to exist
var global = global || window;
</script>
<script src="/src/index.tsx" type="module"></script>
</body>
</html>
It's a fix I have to add to all aws amplify projects and I forgot that I did that since with solid-js I'm not using an index.html anymore same for all react projects using aws: https://github.com/aws-amplify/amplify-js/issues/678 do we have a polyfills.ts in solid-styart like react or angular have @thetarnav ?
thetarnav
thetarnav•13mo ago
no idea I'm not actually sure how to modify the .html file with ssr disabled since it's supposed to be generated either at build or request
Bersaelor
Bersaelor•13mo ago
let's close this one then and open a new more pertitant thread https://discord.com/channels/722131463138705510/1113405856416014389/1113405856416014389 @thetarnav mhmm if you say the .html file is created, there must be some way of modyfing the code that creates the .html to put this one-liner in there first dumb approach was modying entry-client.tsx with
import { mount, StartClient } from "solid-start/entry-client";

mount(() => <StartClient />, document);

var awsFix = document.createElement('script');
awsFix.setAttribute('src','http://example.com/site.js');
document.body.appendChild(awsFix);
import { mount, StartClient } from "solid-start/entry-client";

mount(() => <StartClient />, document);

var awsFix = document.createElement('script');
awsFix.setAttribute('src','http://example.com/site.js');
document.body.appendChild(awsFix);
(but that didn't work) also tried editing node_modules/solid-start/entry-client/mount.tsx but that doesn't really seem to affect the ouput of npm run dev either
Unknown User
Unknown User•13mo ago
Message Not Public
Sign In & Join Server To View
Bersaelor
Bersaelor•13mo ago
hey, so should be an /dist/server/index.mjs file
Bersaelor
Bersaelor•13mo ago
in my case, for AWS, this is the code that runs server-side the client part are the static assets that go into some kind of storage the server part is the code that runs that generates the site the index.html is basically generated by node on the fly, it's not a static file that sits in a folder
Unknown User
Unknown User•13mo ago
Message Not Public
Sign In & Join Server To View
Bersaelor
Bersaelor•13mo ago
in my case (for AWS Lambda), the index.mjs has a .handler method, whose return value is a html response for an index.html
Bersaelor
Bersaelor•13mo ago
i think then you should use the static adapter, like in this tutorial https://dev.to/lexlohr/using-solid-start-with-github-pages-3iok
DEV Community
Using Solid Start with GitHub pages
You may or may not yet have heard about Solid Start, which is the much anticipated upcoming meta...
Bersaelor
Bersaelor•13mo ago
or, as I do too for a static page, just use
export default defineConfig({
plugins: [solid({ ssr: false })],
export default defineConfig({
plugins: [solid({ ssr: false })],
this folder looks very much like the solid-start default output, which is for node
Unknown User
Unknown User•13mo ago
Message Not Public
Sign In & Join Server To View
Bersaelor
Bersaelor•13mo ago
good idea, I mean this one was solved anyway 🙂
Want results from more Discord servers?
Add your server
More Posts
Why does typescript not accept `text() && text().length === x` in solid-start?Is anyone aware why typescript complains about ```ts text() && text().length === 6 ``` in a projecUniversal renderer createElement isn't runningI'm working on a custom renderer and I can't get the createElement function to run. I'm not sure if How to read reactive state from imperative contexts (audio/music)**tl;dr:** is there any major DO NOT guideline around (constantly!) reading reactive states from fuHow to handle auto scrolling in a chat app when new message comes in?So this is an example what i currently have ```jsx const App = () => { const [messages, setMessageHow to get `onMount` to be called on the client for a server-rendered siteI've set up a new project using `solid-start` with `solid-start-aws`. Running `npm run build` createReactivity with normal functionsI try the following: `doubleCount` is using `createMemo` while `tripleCount` is a normal anonymous fSolid-js design system CSS troubleshootingI have a design-system I am building out using Kobalte. However, when I export the components none oHow to throw an Error on an unknown route?I define the content for each route like so: ```jsx return ( <Routes> <Route path={'/foo'} eleJoining SolidJS Vite app with a rest api as single appHello, I'm building a simple front end with the default SolidJS+Vite template. I call this the clienHow to make a sticky navbar change color when it has reached the top? Like in solidjs homepageI think I should use this https://github.com/solidjs-community/solid-primitives/tree/main/packages/i