Skip to content

Commit

Permalink
fix: update URL if Vite reload happens during navigation (#12375)
Browse files Browse the repository at this point in the history
Fixes #12179
  • Loading branch information
Artur- committed Nov 17, 2021
1 parent 9f2b1f2 commit 273b195
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 21 deletions.
Expand Up @@ -212,6 +212,11 @@ public class FrontendUtils {
*/
public static final String INDEX_JS = "index.js";

/**
* File name of Vite helper used in development mode.
*/
public static final String VITE_DEVMODE_TS = "vite-devmode.ts";

/**
* Default Java source folder for OpenAPI generator.
*/
Expand Down
Expand Up @@ -676,6 +676,7 @@ protected FeatureFlags getFeatureFlags() {
TaskGeneratePackageJson.class,
TaskGenerateIndexHtml.class,
TaskGenerateIndexTs.class,
TaskGenerateViteDevMode.class,
TaskGenerateTsConfig.class,
TaskGenerateTsDefinitions.class,
TaskGenerateServiceWorker.class,
Expand Down Expand Up @@ -837,6 +838,11 @@ private void addBootstrapTasks(Builder builder) {
new File(builder.generatedFolder, IMPORTS_NAME),
buildDirectory);
commands.add(taskGenerateIndexTs);
if (builder.getFeatureFlags().isEnabled(FeatureFlags.VITE)
&& !builder.productionMode) {
commands.add(
new TaskGenerateViteDevMode(builder.frontendDirectory));
}
}

private void addGenerateTsConfigTask(Builder builder) {
Expand Down
@@ -0,0 +1,71 @@
/*
* Copyright 2000-2021 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.server.frontend;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.IOUtils;

/**
* Generate <code>vite-devmode.ts</code> if it is missing in frontend/generated
* folder.
* <p>
* For internal use only. May be renamed or removed in a future release.
*
* @since
*/
public class TaskGenerateViteDevMode extends AbstractTaskClientGenerator {

private final File frontendDirectory;

/**
* Create a task to generate <code>index.js</code> if necessary.
*
* @param frontendDirectory
* frontend directory is to check if the file already exists
* there.
* @param generatedImports
* the flow generated imports file to include in the
* <code>index.js</code>
* @param outputDirectory
* the build output directory
*/
TaskGenerateViteDevMode(File frontendDirectory) {
this.frontendDirectory = frontendDirectory;
}

@Override
protected File getGeneratedFile() {
return new File(new File(frontendDirectory, FrontendUtils.GENERATED),
FrontendUtils.VITE_DEVMODE_TS);
}

@Override
protected boolean shouldGenerate() {
return true;
}

@Override
protected String getFileContent() throws IOException {
return IOUtils.toString(
getClass().getResourceAsStream(FrontendUtils.VITE_DEVMODE_TS),
UTF_8);
}

}
@@ -0,0 +1,18 @@
// @ts-ignore
if (import.meta.hot) {
// @ts-ignore
const hot = import.meta.hot;
let pendingNavigationTo: string | undefined = undefined;

window.addEventListener('vaadin-router-go', (routerEvent: any) => {
pendingNavigationTo = routerEvent.detail.pathname + routerEvent.detail.search;
});
hot.on('vite:beforeFullReload', (payload: any) => {
if (pendingNavigationTo) {
// Force reload with the new URL
location.href = pendingNavigationTo;
// Prevent Vite from reloading
payload.path = '/_fake/path.html';
}
});
}
13 changes: 12 additions & 1 deletion flow-server/src/main/resources/vite.generated.ts
Expand Up @@ -121,7 +121,18 @@ export const vaadinConfig: UserConfigFn = (env) => {
attrs: { type: 'module', src: devMode ? '/VAADIN/generated/vaadin.ts' : './generated/vaadin.ts' },
injectTo: 'head'
};
return [vaadinScript];

let scripts = [vaadinScript];

if (devMode) {
const viteDevModeScript: HtmlTagDescriptor = {
tag: 'script',
attrs: { type: 'module', src: '/VAADIN/generated/vite-devmode.ts' },
injectTo: 'head'
};
scripts.push(viteDevModeScript);
}
return scripts;
}
}
},
Expand Down
Expand Up @@ -202,27 +202,9 @@ protected Stream<String> getExcludedPatterns() {
"com\\.vaadin\\.flow\\.server\\.frontend\\.FallibleCommand",
"com\\.vaadin\\.flow\\.server\\.frontend\\.NodeTasks",
"com\\.vaadin\\.flow\\.server\\.frontend\\.NodeUpdater",
"com\\.vaadin\\.flow\\.server\\.frontend\\.TaskCopyFrontendFiles",
"com\\.vaadin\\.flow\\.server\\.frontend\\.TaskCopyLocalFrontendFiles",
"com\\.vaadin\\.flow\\.server\\.frontend\\.TaskGeneratePackageJson",
"com\\.vaadin\\.flow\\.server\\.frontend\\.TaskRunNpmInstall",
"com\\.vaadin\\.flow\\.server\\.frontend\\.TaskUpdateImports(\\$.*)?",
"com\\.vaadin\\.flow\\.server\\.frontend\\.TaskUpdatePackages",
"com\\.vaadin\\.flow\\.server\\.frontend\\.TaskUpdateWebpack",
"com\\.vaadin\\.flow\\.server\\.frontend\\.TaskGenerateEndpointBase",
"com\\.vaadin\\.flow\\.server\\.frontend\\.TaskGenerateFusion",
"com\\.vaadin\\.flow\\.server\\.frontend\\.TaskGenerateOpenAPI",
"com\\.vaadin\\.flow\\.server\\.frontend\\.Task.*",
"com\\.vaadin\\.flow\\.server\\.frontend\\.AbstractTaskClientGenerator",
"com\\.vaadin\\.flow\\.server\\.frontend\\.TaskGenerateTsConfig",
"com\\.vaadin\\.flow\\.server\\.frontend\\.TaskGenerateIndexHtml",
"com\\.vaadin\\.flow\\.server\\.frontend\\.TaskGenerateIndexTs",
"com\\.vaadin\\.flow\\.server\\.frontend\\.TaskGenerateBootstrap",
"com\\.vaadin\\.flow\\.server\\.frontend\\.TaskGenerateTsDefinitions",
"com\\.vaadin\\.flow\\.server\\.frontend\\.TaskGenerateServiceWorker",
"com\\.vaadin\\.flow\\.server\\.frontend\\.TaskInstallWebpackPlugins",
"com\\.vaadin\\.flow\\.server\\.frontend\\.TaskUpdateThemeImport",
"com\\.vaadin\\.flow\\.server\\.frontend\\.EndpointGeneratorTaskFactory",
"com\\.vaadin\\.flow\\.server\\.frontend\\.TaskCopyTemplateFiles",

// Node downloader classes
"com\\.vaadin\\.flow\\.server\\.frontend\\.installer\\.DefaultArchiveExtractor",
Expand Down
Expand Up @@ -80,7 +80,8 @@ Path toPath() {
static class GeneratorFileVisitor extends SimpleFileVisitor<Path> {
private static final List<String> filesToKeep = Arrays.asList(
ClientAPIGenerator.CONNECT_CLIENT_NAME,
FrontendUtils.BOOTSTRAP_FILE_NAME, FrontendUtils.INDEX_TS,
FrontendUtils.BOOTSTRAP_FILE_NAME,
FrontendUtils.VITE_DEVMODE_TS, FrontendUtils.INDEX_TS,
FrontendUtils.INDEX_JS, FrontendUtils.THEME_IMPORTS_NAME,
FrontendUtils.THEME_IMPORTS_D_TS_NAME);
private final Logger logger;
Expand Down

0 comments on commit 273b195

Please sign in to comment.