Skip to content

Side-effect-free imports? #21

Closed
Closed

Description

@Vinnl

I understand (and support) that this library is aimed at the browser only, however, I am writing an app for which I do an initial run at build time, to generate HTML that can be displayed while client-side JS is being parsed and executed. I am trying to make sure that I'm not calling new Session at this point in time (because there's no browser environment yet), however, even just calling

import { Session } from "@uvdsl/solid-oidc-client-browser";

already throws this error:

self is not defined

which I think (though the stack trace doesn't tell me) is caused by this line:

self.onconnect = (event: MessageEvent) => {

And that, I think, is running because of this import:

import { RefreshMessageTypes } from './RefreshWorker';

Would it be possible to defer setting that onconnect listener to when it's actually needed, e.g. when a Session's constructor is being called? Or perhaps just to move RefreshMessageTypes to a separate module?

Activity

  1. uvdsl commented on Nov 4, 2025

    @uvdsl
    Owner

    What is your setup? How can I reproduce the error you are facing?

    I think this one is easy to solve - if it is really only the import. But I would like to check before asking you to go through the trouble of testing it for me.

    Would it be possible to defer setting that onconnect listener to when it's actually needed

    Well, I am not too happy with that idea - I think this might get messy because sessions keep overwriting stuff, file structure gets more complicated, etc. If it is just the enum import, no problem, I can extract that.

  2. added a commit that references this issue on Nov 4, 2025
  3. Vinnl commented on Nov 4, 2025

    @Vinnl
    Author

    Oops sorry, bug reporting 101 🤦 Here's a minimal reproduction:

    git clone [email protected]:Vinnl/socb-repro.git;
    cd socb-repro;
    npm install;
    npm run dev;

    If you then open the given website (usually http://localhost:3000), then you'll see "1 issue" in the bottom left-hand corner:

    Image

    If you click that, you'll see the actual error self is not defined:

    Image

    And note that I'm currently actually instantiating a Session object, but if you go one commit back, you can see that even just logging it is enough to trigger the error.

    I do think that just extracting the enum would be sufficient 🤞

  4. uvdsl commented on Nov 4, 2025

    @uvdsl
    Owner

    Thanks, I'll check it out!

  5. angelo-v commented on Nov 5, 2025

    @angelo-v

    The fact that the lib automatically tries to register a SharedWorker is also a problem for Web apps, that load this lib (or another lib using it, like @pod-os/elements in my case) from CDN. Due to cross origin restrictions registering the SharedWorker fails.

    The problem is not the RefreshWorker.ts itself, but the fact, that this script is registered as a worker here.

    In my understanding the lib should just provide the RefreshWorker.ts, but leave registration to the app. This means there are a few more lines of code to setup the app, but you gain the flexibility of using it without the worker or with a worker that actually is created on the same origin.

    I am following a similar pattern with the service worker in PodOS. The worker setup is provided by a lib here:

    https://github.com/pod-os/PodOS/blob/main/service-worker/src/setupServiceWorker.ts

    But is actually called in a thin layer script on same origin as the actual app:

    https://github.com/pod-os/PodOS/blob/main/apps/service-worker.js

  6. uvdsl commented on Nov 5, 2025

    @uvdsl
    Owner

    @angelo-v , the lib does not automatically try to register a worker.
    When you create a session, then the worker is automatically created for you.

    What you are describing is a workaround for CORS issues when pulling the library from the CDN, thank you for raising this.
    I could extend the SessionOptions to allow for an app-created SharedWorker to be passed to the session to solve that problem.

    However, this is a different issue and not related to the side-effect of the import (of the session class) itself.


    @Vinnl, thank you, I could reproduce the error. Extracting the enum seems to do the trick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions