Clarify usage and point of InitialState[Service]

Can someone shed light on what the InitialStateService is all about or rather why we need it?

For example, in this part of the catgifs tutorial, it is used in the controller action that serves the HTML for the app’s main page: Nextcloud

The relevant code is basically this:

```
	public function mainPage(): TemplateResponse {
		$fileNameList = $this->getGifFilenameList();
		$fixedGifSize = $this->config->getUserValue($this->userId, Application::APP_ID, self::FIXED_GIF_SIZE_CONFIG_KEY);

		$myInitialState = [
			'file_name_list' => $fileNameList,
			self::FIXED_GIF_SIZE_CONFIG_KEY => $fixedGifSize,
		];
		$this->initialStateService->provideInitialState('tutorial_initial_state', $myInitialState);

		$appVersion = $this->config->getAppValue(Application::APP_ID, 'installed_version');
		return new TemplateResponse(
			Application::APP_ID,
			'myMainTemplate',
			[
				'app_version' => $appVersion,
			]
		);
	}
```

I figure that this code could just as well provide this initial state straight to the template instead. The only reason I can think of right now to use the InitialStateService is to store the initial state in a common place and then have the JS for the page retrieve it “on demand” (like shown here. I realize this can be useful in other situations that are more complex than this one in the tutorial.

But is there anything else to it, or is it really nothing more than a common storage for some initial data (seeing as it doesn’t seem to persist anything, so it will need to be seeded on each request)?

FWIW, it’s badly undocumented in the API docs.

I think you got it completely right. Initial state is a way to pass data from the backend to the frontend on initial page load without having to send a separate request. That’s it.

Thanks, that’s reassuring :slight_smile: