Location is None when initializing with pn.serve (but works when served from commandline)

I have a successfully working Panel multi-page application that can be run via commandline using panel serve page1.py page2.py page3.py

I thought I would refactor this into a single app.py to programmatically launch my multipage Panel application. The application looks something like this:

import views
...

pn.extension("tabular")

page1 = views.Page1()
page2 = views.Page2()
page3 = views.Page3()

pn.serve({
    "page1":page1.get_page,
    "page2":page2.get_page,
    "page3":page3.get_page,
})

where each Page is a Python class with a defined function get_page that returns a template object.

Since I want to provide some session state for individual users via url, I sync the pn.state.location url with one of the current widgets/variables:

# in views.py
class Page1:
    def __init__(self,*args,**kwargs):
        ...
        self.variable = pn.widgets.Select(...)
        pn.state.location.sync(self.variable, {"value":"variable"})
    ...

This works fine when using panel serve however, when refactoring into the singular app.py listed above and running python -m app.py via commandline, I am met with the following error:

Traceback (most recent call last):
  File "~/app.py", line 13, in <module>
    page1 = views.Page1()
                   ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sundeep/moi/work/safeyou/app/views.py", line 52, in __init__
    pn.state.location.sync(self.variable, {"value":"variable"})
    ^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'sync'

Which suggests that pn.state.location is not initialized. Does anyone have any suggestions how to go about amending this or what the issue might be?

Try adding an if pn.state.location

Thanks @ahuang11 , this works however leaves the option of syncing unavailable the first time the page is loaded. Do you know any way pn.state.location could be initialized beforehand?

I think you might want to file an issue