Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the persisted viewmodel in Chrome #687

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -35,6 +35,11 @@ protected override void RenderControl(IHtmlWriter writer, IDotvvmRequestContext
writer.AddAttribute("value", serializedViewModel);
writer.RenderSelfClosingTag("input");


writer.AddAttribute("type", "hidden");
writer.AddAttribute("id", "__dot_persisted_viewmodel_root");
writer.RenderSelfClosingTag("input");

// init on load
writer.RenderBeginTag("script");
writer.WriteUnencodedText($@"
Expand Down
5 changes: 5 additions & 0 deletions src/DotVVM.Framework/Resources/Scripts/DotVVM.d.ts
Expand Up @@ -250,6 +250,10 @@ interface IDotvvmViewModelInfo {
renderedResources?: string[];
url?: string;
virtualDirectory?: string;
resultIdFragment?: string;
resources?: {
[name: string]: boolean;
};
}
interface IDotvvmViewModels {
[name: string]: IDotvvmViewModelInfo;
Expand Down Expand Up @@ -303,6 +307,7 @@ declare class DotVVM {
extensions: IDotvvmExtensions;
useHistoryApiSpaNavigation: boolean;
isPostbackRunning: KnockoutObservable<boolean>;
private isBrowserReload;
init(viewModelName: string, culture: string): void;
private handlePopState;
private handleHashChangeWithHistory;
Expand Down
39 changes: 37 additions & 2 deletions src/DotVVM.Framework/Resources/Scripts/DotVVM.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/DotVVM.Framework/Resources/Scripts/DotVVM.min.js

Large diffs are not rendered by default.

55 changes: 53 additions & 2 deletions src/DotVVM.Framework/Resources/Scripts/DotVVM.ts
Expand Up @@ -26,6 +26,8 @@ interface IDotvvmViewModelInfo {
renderedResources?: string[];
url?: string;
virtualDirectory?: string;
resultIdFragment?: string;
resources?: { [name: string]: boolean };
}

interface IDotvvmViewModels {
Expand Down Expand Up @@ -218,11 +220,43 @@ class DotVVM {
public useHistoryApiSpaNavigation: boolean;
public isPostbackRunning = ko.observable(false);

private isBrowserReload(): boolean {
if (performance) {
if (performance.getEntriesByType) {
var entries = performance.getEntriesByType("navigation");

if (entries.length > 0) {
return (<PerformanceNavigationTiming>entries[0]).type === "reload";
}
}

// deprecated in Navigation Timing Level 2 specification
if (performance.navigation) {
return performance.navigation.type === 1;
}
}

return false;
}

public init(viewModelName: string, culture: string): void {
this.addKnockoutBindingHandlers();

// load the viewmodel
var thisViewModel = this.viewModels[viewModelName] = JSON.parse((<HTMLInputElement>document.getElementById("__dot_viewmodel_" + viewModelName)).value);
var persistedJson = (<HTMLInputElement>document.getElementById("__dot_persisted_viewmodel_" + viewModelName)).value;
var json: string;

if (persistedJson) {
json = persistedJson;
} else if (!this.isBrowserReload() && history.state && history.state.dotvvm_viewmodels && history.state.dotvvm_viewmodels[viewModelName]) {
json = history.state.dotvvm_viewmodels[viewModelName];
} else {
json = (<HTMLInputElement>document.getElementById("__dot_viewmodel_" + viewModelName)).value;
}

var thisViewModel = thisViewModel = <IDotvvmViewModelInfo>JSON.parse(json);
this.viewModels[viewModelName] = thisViewModel;

if (thisViewModel.resources) {
for (var r in thisViewModel.resources) {
this.resourceSigns[r] = true;
Expand Down Expand Up @@ -347,14 +381,31 @@ class DotVVM {

private persistViewModel(viewModelName: string) {
var viewModel = this.viewModels[viewModelName];

var persistedViewModel = {};
for (var p in viewModel) {
if (viewModel.hasOwnProperty(p)) {
persistedViewModel[p] = viewModel[p];
}
}
persistedViewModel["viewModel"] = this.serialization.serialize(persistedViewModel["viewModel"], { serializeAll: true });
(<HTMLInputElement>document.getElementById("__dot_viewmodel_" + viewModelName)).value = JSON.stringify(persistedViewModel);

var json = JSON.stringify(persistedViewModel);
var currentState = history.state ? history.state : {};
var persistedViewModels = currentState.dotvvm_viewmodels ? currentState.dotvvm_viewmodels : {};

// add the new viewmodel to the existing state, otherwise SPA mode will break.
var state = {
...currentState,
dotvvm_viewmodels: {
...persistedViewModels,
[viewModelName]: json
}
};

(<HTMLInputElement>document.getElementById("__dot_viewmodel_" + viewModelName)).value = json;
(<HTMLInputElement>document.getElementById("__dot_persisted_viewmodel_" + viewModelName)).value = json;
history.replaceState(state, document.title);
}

private backUpPostBackConter(): number {
Expand Down