Giter Club home page Giter Club logo

Comments (9)

cfitz avatar cfitz commented on May 25, 2024 3

I ran into the same problem when i switched over my project to use yarn...I removed the now-logs dependency, which includes the xmlhttprequest-ssl package. You need that one...the ssl version: https://www.npmjs.com/package/xmlhttprequest-ssl
not the older xmlhttprequest package.

from nextjs-starter.

mulikaminker avatar mulikaminker commented on May 25, 2024

My app run on heroku. the strange thing is that when a user is logging and have a token in the local storage, the app crashes every some days and the error in the console is "XMLHttpRequest is not defined". it's possible to replace the XMLHttpRequest with fetch or axios?

from nextjs-starter.

mulikaminker avatar mulikaminker commented on May 25, 2024

I see now that when i remove the connect.sid cookie the app back to normal. any ideas?

from nextjs-starter.

iaincollins avatar iaincollins commented on May 25, 2024

Hmm I'm not sure what's going on here, I'll see if I can replicate on Heroku. It shouldn't be calling XMLHttpRequest on the server ever. Maybe something Heroku specific is going on.

The reason it uses XMLHttpRequest rather than fetch is interesting, it's actually to do with having HTTP Only cookies (that can't be read in JavaScript) while still supporting universal rendering.

It does this to provide a "best practice" example but actually it complicates authentication significantly,
and I'm considering if it's worth the hassle of supporting.

I'll investigate and get back to you.

from nextjs-starter.

iaincollins avatar iaincollins commented on May 25, 2024

Hi @kamish24six - tried deploying to Heroku, found an issue and fixed it - it's now working on Heroku.

The issue seemed to be that Heroku didn't know to run "next build" before running "next start" (required on production for Next.js apps).

I added "postinstall": "next build" to the package.json file under scripts.

The scripts section in package.json now looks like this:

  "scripts": {
    "test": "xo",
    "dev": "NODE_ENV=development PORT=3000 node index.js",
    "build": "next build",
    "start": "node index.js",
    "postinstall": "next build"
  },

I've confirmed I was able to deploy directly from GitHub to Heroku and log in so it should work for you now.

Note you will need to configure authentication via env vars. It's probably easiest to configure email authentication first, just to check it's all working. You don't need to configure a user database just for testing, but if you don't the database will just be erased every time you restart the service.

from nextjs-starter.

mulikaminker avatar mulikaminker commented on May 25, 2024

Thanks. In the past I succeeded to upload the project to heroku, and it works.Oftentimes it can suddenly stop working and and an XMLHttpRequest error appears. When ן delete the connect.sid cookie is starts working again.

from nextjs-starter.

iaincollins avatar iaincollins commented on May 25, 2024

Interesting! Thanks for the added detail.

I'm going to re-open this and see if I can replicate the problem.

from nextjs-starter.

iaincollins avatar iaincollins commented on May 25, 2024

Thanks to your bug report I think I have a fix for this rolled out now!

I did a review of all calls to XMLHttpRequest in the project (they are all in components/session.js) and there was one place where XMLHttpRequest could accidentally be called while running on the server (which it should never be).

It could happen when the client had an existing session, but it had expired on the server and then the page was reloaded on the client (e.g. with refresh, or by visiting the site again after a period of being idle). This explains why it didn't happen right away.

I do want to refactor this code, and I did get a pull request for that, but there were some issue with the PR and I had to revert the change unfortunately. Most of the complexity is caused by the code supporting HTTP Only cookies.

I will see what the browser support for fetch with credentials: 'same-origin' is like to see if it works with HTTP Only cookies in browsers now, and if so trying and merge that pull request back in - the code isn't that complex, but fetch is a terser and easier to read and debug.

Thanks for the bug report!

I'd appreciate any feedback if you still see this issue with version 4.2.7

from nextjs-starter.

iaincollins avatar iaincollins commented on May 25, 2024

Addendum: I've actually just gone ahead and refactored the Session class to use fetch now.

from nextjs-starter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.