-
Notifications
You must be signed in to change notification settings - Fork 67
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
window is not defined #186
Comments
Hi @xxyyzz2050 , Could you give us a bit more information? What step in the transpiration process are you at? What is the full stack trace? Thanks. |
ok, I use Nuxt framework & latest version of Node.js and npm |
@jsflax I imagine that this might be due to using the SDK for SSR. I'm having the same issue with Next.js. If fails on this line:
This would be expected since
showing an error due to the missing dependency:
Are the SDKs expected to support SSR? Is there a workaround for this or is Stitch not an option with SSR? Thanks. |
I've been trying to figure out how Stitch can be used with SSR as well, particularly NextJS. Would one have to instantiate both a Stitch-Server SDK instance and a Stitch-Browser SDK instance, use the server instance if !window and use the browser instance if there is? This also presents an issue with setting up sessions so that initial requests for logged in users can be server rendered. As an example of an alternative Auth flow, the official NextJS + Firebase Auth example works like this: 1) Login occurs client-side, 2) Request an Auth Token from Firebase client-side, 3) Pass Auth token to /login.js route, 4) Use Firebase to decode token, 5) Set HTTP Only cookie session with user info. Every subsequent request is immediately authenticated. I'm not sure how this can be done with Stitch. Auth Token gets stored in local storage, but is there a way to get it? And if you could deliver it to the server, could you use it with Stitch-Server SDK to authenticate user server side and create a session? |
I'm getting the same issue with Next.js |
I used a stateful component that waits until the mount lifecycle (I'm using react hooks, useEffect) which only loads once the component mounts. This solves the problem for me in Next.js.. won't help with anything server side rendering... |
In the future, we hope to have a universal JS SDK. However, in the interim, @agstover is right. A solution could be to make your own isomorphic multiplexer file, i.e., an |
+1 for SSR support |
Trying to use Next.js with Stitch requires SSR support. Even one of the fetch dependancies isn't written for SSR so an alternative to whatwg-fetch would be better? |
Hi @phillipmalboeuf and @Ruegen , have you tried using Let us know if that would be sufficient. Otherwise, we can look into offering a more general solution. The problem with bundling a polymorphic fetch library in the browser SDK is the fact that some JS frameworks (such as NativeScript) will not let you bundle JavaScript libraries that contain NodeJS-based dependencies. See #207 |
Thanks @adamchel, using
The current system setup isn't SSR capable, and it's not just a matter of the fetch library. I believe the credentials storage method would need to be polymorphic as well. Update |
Hi @adamchel, yes I’ve tried it however like Phillip I got the same errors. I got around it by only loading the package in on the client side - which involves a fair bit of trickery (not pretty), the server side can’t use the client side and vice versa. It would be better to have it load for SSR which would resolve a great deal of issues. @phillipmalboeuf I believe the client side uses localstorage to save the credentials automatically. |
Thanks for the feedback everyone! So, it's hard for us to provide truly isomorphic networking and storage because new javascript libraries with new requirements and nuances pop up all the time, and it's difficult for us to keep up. In the past we actually switched to an isomorphic fetch library to support more JS libraries, but then it caused problems with other JS libraries. This is just the state of the JS ecosystem 🤷♂️. That's why we've generally opted to provide the most basic storage and networking capabilities. If you'd like to go on a deeper dive, it might be useful for you to know that we have abstracted the storage and network layer of the SDK into simple See:
If you know the limitations/requirements of the framework you're working with, you can implement a import { Stitch, StitchAppClientConfiguration } from "mongodb-stitch-browser-sdk";
// these currently do not exist
import { IsomorphicNetworkTransport } from "my-stitch-isomorphic-network-transport";
import { IsomorphicStorage } from "my-stitch-isomorphic-storage";
let client = Stitch.initializeAppClient(
"your-client-app-id",
new StitchAppClientConfiguration.Builder()
.withTransport(new IsomorphicNetworkTransport())
.withStorage(new IsomorphicStorage())
.build()
); You can look at our existing Storage and Transport implementations as a reference, and we'd be happy to answer any questions you may have or any issues that come up: Although this is something we'd personally like to work more on, we are juggling many other priorities. Building your own Transport and Storage (and opening a PR if you'd like) will probably be the fastest way to get the SDK working in some of these third party libraries where isomorphic libraries are necessary. If you'd like to have a say in Stitch's product roadmap and the features that we should work on, please add a new issue or vote on existing issues at https://mongodb.canny.io/mongodb-stitch ! |
Oh interesting @adamchel, didn't realize the |
@adamchel The problem is that even importing the packages With that said I've tried building a client still with |
Is there a way to use |
What are the SSR issues when just using |
@adamchel the dependencies are a good place to start. import { Stitch } from 'mongodb-stitch-browser-sdk'
import React from 'react'
import ReactDOM from 'react-dom/server'
const client = Stitch.initializeDefaultAppClient('****')
const html = ReactDOM.renderToString(<div>
{client.auth.isLoggedIn.toString()}
</div>)
console.log(html) Running this in node returns a |
Hey @adamchel, @Ruegen, @maredudd, @agstover, so I got to a weird solution for this. It needs a couple of steps (note that I'm writing TypeScript and bundling with Parcel): First, change all your references to the stitch sdk to Second, in your
Third, if you're on TS add to your
And VOILÀ, bundling this for the browser and bundling this for Node and running those works just fine! I haven't tried this with Next.js but I imagine it might work, let me know. |
And for those on next.js, you can do it the same way but you'll need to add a webpack alias resolve to your
Let me know! |
Hey @phillipmalboeuf thanks for taking a look and finding a workaround for this! I'm sure this will be very helpful for a lot of users. We still would like to eventually have a more clean and robust solution for SSR, so I'll leave this issue open until we get a chance to investigate this further. |
We are using VueJS/NuxtJS for our project. We have a rest api with MongoDB/Mongoose, but we would really like to skip the server part and just use Nuxt with MongoDB Stitch. But we are getting the same error with “self” not defined. |
I can verify that the workaround @phillipmalboeuf discovered also work with Nuxt js with the following small modification. In the nuxt.config.js file: Looking forward to the day where the SDK supports SSR. |
@Mattertorian I'm curious to see how integrating Stitch into your NuxtJS app worked out for you. I'm also looking to use Stitch with NuxtJS and the Google maps API, so I can send directions to the serve and it can return relevant information from the database. |
I didn't read this whole thread, but I have stitch working with SSR (angular universal), by switching the following package dependencies:
To:
4.4.0 still has the problem.
to mongodb-stitch-browser-core. If they can get by with the ^2.0.4 version, then they can circumvent that libraries improper access to the self var, when just loading the library. For a fetch issue reference see: JakeChampion/fetch#657 |
I am trying to do this with next.js . Where should I initialize the stitch app? Currently, I am getting error - default app can only be set once; currently set to 'my app id' |
I think the way stitch currently works needs to be changed, It needs to be 100% API and no SDK, So Login/Registration and so on needs to have a REST endpoints out of the box, (If GraphQL activated Mutations out of the box for Login, Register etc...) |
Need to understand if you use any of the build in authentication methods in Stitch, you can conclude it's easier to do it client side, and load the sdk using nextjs dynamic loading. Stitch maintains the session in order to provide you with all the authenticated goodies like customised access to the functions and attached clusters. If you really need SSR option, the easiest way for you is to do custom authentication, and send to Stitch for custom JWT authentication. Don't waste development time trying for workarounds. Stitch is serverless, I ditched SSR to go full serverless. Response time for Client > Stitch > Atlas > Stitch > Client is much faster than Client > NodeJs > Stitch > Atlas > Stitch > NodeJS > Client. |
I have not tested it yet but hitting the Stitch Client API directly should solve many problems in Nextjs, https://github.com/mongodb/stitch-specifications/blob/master/source/sdks/sdks.rst#userpasswordauthproviderclient. We could create Nextjs API routes in the /api folder, for example, nextapp.com/api/login In this route, we send a request to https://stitch.mongodb.com/api/client/v2.0/app/<client_app_id>/auth/providers/<provider_name>/login the response will be access_token, refresh_token, user_id, device_id that we can handle then, has anyone an idea what the best method would be at this point to store or handle it? |
@ivanjeremic I've been looking everywhere for all of the auth API endpoints, thank you! These should be in the official documentation. As for where to store it, I've been storing them in cookies. I store the user data, access_token and expiry date in one cookie, and the refresh_token in an http-only cookie. Then, I setup a /refresh-token endpoint that my client hits whenever it expires to get the new access_token (and updates the cookie). You can refresh the token by sending a post request to |
The documentation is really horrible in Stitch, no idea how they want to compete with Strapi or Hasura with no community chat & bad documentation, if after rebranding to Realm nothing gets better I don't see it becoming a thing. All they have is a Forum where you need the approval of your posts, they need a chat on Discord or Slack. |
I'm using babel transpiler
//error: window is not defined
The text was updated successfully, but these errors were encountered: