Initializing a service worker before rewrite in Next.js.
Unanswered
West African Crocodile posted this in #help-forum
West African CrocodileOP
Hi. I'm looking for a way to initialize the service worker before the user gets redirected to an external application. I'm using rewrites on a specific path to display an external application that handles user registration/login. I communicate with the external application using a service worker. Using sw external app sends me a token, on my side, I validate the token and perform other operations. Finally, I send a message back to the external application through the service worker, indicating that everything is okay, and request to be redirected back to my application at a specific URL.
If the navigation to a specific path with rewrites set happens within my application's navigation, there is no problem because I have the service worker initialization set up in _app. My question is how to handle this situation when someone directly accesses a path that is rewritten. In that case, the service worker won't be initialized, and I won't be able to capture the information sent by the external application. Any ideas? Thanks!
If the navigation to a specific path with rewrites set happens within my application's navigation, there is no problem because I have the service worker initialization set up in _app. My question is how to handle this situation when someone directly accesses a path that is rewritten. In that case, the service worker won't be initialized, and I won't be able to capture the information sent by the external application. Any ideas? Thanks!
3 Replies
West African CrocodileOP
No one?
Uhm... I don't think you can do that. The redirection is done server side... How are you initialising it if the user doesn't load your application on their browser?
West African CrocodileOP
thats the point. well, what comes to mind is creating two pages: one main page and one auxiliary page. On the main page, I'll have a rewrite set up. On the auxiliary page, which will also be accessible from the navigation, I'll have a client-side router push to the main page in the useEffect. I'll also have the initialization of the service worker set up. However, the user experience will be worse. Alternatively, on the external application side, someone landed in their application, and they check if the service worker is functioning. If not, they push back to my application, specifically to the auxiliary page that initializes the service worker and redirects to my main page