Webpage loading optimization method and device, equipment and readable storage medium

By analyzing and splitting web page files and optimizing loading order and file format, the problem of slow website loading speed is solved, and faster page loading time and user experience is achieved.

CN118939898APending Publication Date: 2024-11-12UBTECH ROBOTICS CORP LTD

Patent Information

Application Number
CN202410982712.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-19
Publication Date
2024-11-12

AI Technical Summary

Technical Problem

When a website loads high-resolution pictures or video materials, the first content is drawn for a long time, the script execution time and total blocking time are long, which affects the user experience.

Method used

Get the first content drawing time through the web analysis tool, analyze the dependencies of language files and cascading style sheet files, split it into sub-files, and set the loading order through the front-end resource construction tool, delay loading non-initial files, compress and obfuscate files, use Base64 encoding or file loader optimizes image loading.

Benefits of technology

Reduces the initial loading time of the website, improves page response speed, and improves user experience.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118939898A_ABST
    Figure CN118939898A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a webpage loading optimization method and device, equipment and a readable storage medium, and belongs to the field of webpage loading. The method comprises the following steps: acquiring first content drawing time through a webpage analysis tool; if the first content drawing time is greater than a first preset threshold value, analyzing a dependency relationship between the language file and the cascading style sheet file; splitting the language file and the cascading style sheet file according to the dependency relationship to obtain a language sub-file and a cascading style sheet sub-file; setting a loading sequence of the language sub-files and the cascading style sheet sub-files through a front-end resource construction tool, and delaying loading of the non-initial language sub-files and the non-initial cascading style sheet sub-files through the loading sequence; and loading the initial language sub-file and the initial cascading style sheet sub-file according to the loading sequence. A language file and a cascading style sheet file are segmented through a front-end resource construction tool, and sub-files required by a current page are loaded as required, so that the initial loading time is shortened.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of web page loading, and in particular to a web page loading optimization method, device, equipment and readable storage medium. Background Art

[0002] The performance score of the currently developed website is low. If a large number of high-resolution images or video materials are used in the website, the loading and rendering of the materials will consume a lot of time and resources. Usually, the main problem is that the First Content Paint (FCP) time is long, that is, the website loads slowly and the white screen time is long; when loading the page, the script execution time and the operation time that blocks the main thread are long, that is, the total blocking time (TBT) is long, which affects the user experience. Summary of the invention

[0003] In order to solve the above technical problems, the embodiments of the present application provide a web page loading optimization method, device, equipment and readable storage medium.

[0004] In a first aspect, an embodiment of the present application provides a method for optimizing web page loading, the method comprising:

[0005] Get the first contentful paint time through web analytics tools;

[0006] If the first content drawing time is greater than a first preset threshold, analyzing the dependency relationship between the language file and the cascading style sheet file;

[0007] Splitting the language file and the cascading style sheet file according to the dependency relationship to obtain a language sub-file and a cascading style sheet sub-file;

[0008] Setting the loading order of the language sub-file and the cascading style sheet sub-file by a front-end resource construction tool, wherein the language sub-file includes an initial language sub-file and a non-initial language sub-file, and the cascading style sheet sub-file includes an initial cascading style sheet sub-file and a non-initial cascading style sheet sub-file;

[0009] Delay loading of the non-initial language sub-file and the non-initial cascading style sheet sub-file by the loading sequence;

[0010] The initial language sub-file and the initial cascading style sheet sub-file are loaded in the loading sequence.

[0011] In one embodiment, the step of splitting the language file and the cascading style sheet file according to the dependency relationship to obtain a language sub-file and a cascading style sheet sub-file includes:

[0012] Obtaining the dependency relationship between the language file and the cascading style sheet file;

[0013] Determining a split point between the language file and the cascading style sheet file according to the dependency relationship;

[0014] According to the split point, a corresponding language sub-file and cascading style sheet sub-file are created.

[0015] In one embodiment, the delaying of loading the non-initial language sub-file and the non-initial cascading style sheet sub-file by the loading sequence includes:

[0016] Delayed loading of the non-initial language sub-file by dynamic import of the front-end resource construction tool;

[0017] The non-initial cascading style sheet sub-file is lazily loaded by the cascading style sheet loader of the front-end resource construction tool.

[0018] In one implementation, the initial language sub-file includes a picture to be loaded, and loading the initial language sub-file according to the loading sequence includes:

[0019] Compressing the to-be-loaded picture into a network graphics format or a scalable vector graphics format by the front-end resource construction tool;

[0020] The compressed image is loaded through the uniform resource locator loader or the file loader of the front-end resource construction tool.

[0021] In one embodiment, the step of loading the compressed image through a uniform resource locator loader or a file loader of the front-end resource construction tool includes:

[0022] Determining, by the front-end resource construction tool, whether the size of the compressed image is less than a third preset threshold;

[0023] If the size of the picture is smaller than a third preset threshold, configuring the uniform resource locator loader to convert the compressed picture into Base64 encoding and embed it into the language file or the cascading style sheet file;

[0024] If the size of the image is greater than a third preset threshold, the file loader is configured to output the compressed image to a specified directory.

[0025] In one implementation, after obtaining the first content drawing time through the webpage analysis tool, the method further includes:

[0026] Respectively obtaining the size of the language file and the size of the cascading style sheet file, and setting a corresponding fourth preset threshold and a fifth preset threshold;

[0027] If there is a language file whose size is larger than the fourth preset threshold or there is a cascading style sheet file whose size is larger than the fifth preset threshold, configuring a code compression plug-in through the front-end resource construction tool;

[0028] The target language file or the target cascading style sheet file is compressed by the code compression plug-in, and the target language file or the target cascading style sheet file is obfuscated.

[0029] In one embodiment, compressing the target language file or the target cascading style sheet file by the code compression plug-in includes:

[0030] Compressing the target language file by using the code compression plug-in to remove useless codes and comments;

[0031] Compressing the target cascading style sheet file by using the code compression plug-in to remove redundant styles and invalid attributes;

[0032] The unused target language files or target cascading style sheet files are automatically removed by the code compression plug-in.

[0033] In a second aspect, an embodiment of the present application provides a web page loading optimization device, the web page loading optimization device comprising:

[0034] The acquisition module is used to obtain the first content painting time through the web page analysis tool;

[0035] A judgment module, configured to analyze the dependency relationship between the language file and the cascading style sheet file if the first content drawing time is greater than a first preset threshold;

[0036] A splitting module, used for splitting the language file and the cascading style sheet file into language sub-files and cascading style sheet sub-files according to the dependency relationship;

[0037] A setting module, used for setting the loading order of the language sub-file and the cascading style sheet sub-file through a front-end resource construction tool, the language sub-file includes an initial language sub-file and a non-initial language sub-file, and the cascading style sheet sub-file includes an initial cascading style sheet sub-file and a non-initial cascading style sheet sub-file;

[0038] A delay module, used for delaying the loading of the non-initial language sub-file and the non-initial cascading style sheet sub-file according to the loading sequence;

[0039] A loading module is used to load the initial language sub-file and the initial cascading style sheet sub-file according to the loading sequence.

[0040] In a third aspect, an embodiment of the present application provides an electronic device, including a memory and a processor, wherein the memory is used to store a computer program, and when the computer program is run by the processor, the web page loading optimization method provided in the first aspect is executed.

[0041] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium storing a computer program, which executes the web page loading optimization method provided in the first aspect when running on a processor.

[0042] The webpage loading optimization method provided by the present application above obtains the first content drawing time through a webpage analysis tool; if the first content drawing time is greater than a first preset threshold, the dependency relationship between the language file and the cascading style sheet file is analyzed; the language file and the cascading style sheet file are split according to the dependency relationship to obtain a language sub-file and a cascading style sheet sub-file; the loading order of the language sub-file and the cascading style sheet sub-file is set through a front-end resource construction tool, the language sub-file includes an initial language sub-file and a non-initial language sub-file, and the cascading style sheet sub-file includes an initial cascading style sheet sub-file and a non-initial cascading style sheet sub-file; the non-initial language sub-file and the non-initial cascading style sheet sub-file are delayed to load through the loading order; the initial language sub-file and the initial cascading style sheet sub-file are loaded through the loading order. The language file and the cascading style sheet file are split through the front-end resource construction tool, and the sub-files required for the current page are loaded on demand to reduce the initial loading time. BRIEF DESCRIPTION OF THE DRAWINGS

[0043] In order to more clearly illustrate the technical solution of the present application, the following is a brief introduction to the drawings required for use in the embodiments. It should be understood that the following drawings only illustrate certain embodiments of the present application and should not be regarded as limiting the scope of protection of the present application. In each of the drawings, similar components are numbered similarly.

[0044] Figure 1 A schematic diagram of a process of a web page loading optimization method provided by an embodiment of the present application is shown;

[0045] Figure 2 Another schematic diagram of the process of optimizing the webpage loading method provided by the embodiment of the present application is shown;

[0046] Figure 3 Another schematic diagram of the process of the webpage loading optimization method provided by the embodiment of the present application is shown;

[0047] Figure 4 A structural schematic diagram of a web page loading optimization device provided in an embodiment of the present application is shown.

[0048] Icons: 400-webpage loading optimization device, 401-acquisition module, 402-judgment module, 403-splitting module, 404-setting module, 405-delay module, 406-loading module. DETAILED DESCRIPTION

[0049] The technical solutions in the embodiments of the present application will be described clearly and completely below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, rather than all of the embodiments.

[0050] The components of the embodiments of the present application generally described and shown in the drawings herein may be arranged and designed in various configurations. Therefore, the following detailed description of the embodiments of the present application provided in the drawings is not intended to limit the scope of the application claimed for protection, but merely represents the selected embodiments of the present application. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without making creative work belong to the scope of protection of the present application.

[0051] Hereinafter, the terms "including", "having" and their cognates, which may be used in various embodiments of the present application, are intended only to indicate specific features, numbers, steps, operations, elements, components, or combinations of the foregoing items, and should not be understood as first excluding the existence of one or more other features, numbers, steps, operations, elements, components, or combinations of the foregoing items or adding the possibility of one or more features, numbers, steps, operations, elements, components, or combinations of the foregoing items.

[0052] Furthermore, the terms “first”, “second”, “third”, etc. are merely used for distinguishing descriptions and are not to be understood as indicating or implying relative importance.

[0053] Unless otherwise defined, all terms (including technical terms and scientific terms) used herein have the same meanings as those generally understood by those skilled in the art to which the various embodiments of the present application belong. The terms (such as those defined in generally used dictionaries) will be interpreted as having the same meanings as the contextual meanings in the relevant technical field and will not be interpreted as having idealized meanings or overly formal meanings unless clearly defined in the various embodiments of the present application.

[0054] Example 1

[0055] The embodiment of the present application provides a web page loading optimization method, which realizes web page loading optimization by configuring a front-end resource construction tool.

[0056] See also Figure 1 , the web page loading optimization method includes steps S101-S106:

[0057] S101: Obtaining the first content drawing time through a web page analysis tool.

[0058] In this embodiment, the web page analysis tool Lighthouse is used to analyze the web page. In terms of the performance (Performance) of the web page, key indicators such as the first contentful paint (FCP), the first meaningful paint (FMP) and the total blocking time (TBT) are included. A performance evaluation report on the web page is obtained through Lighthouse, and the indicator scores for each aspect will be listed in detail in the report.

[0059] It should be noted that Lighthouse also includes other aspects of web page analysis, including accessibility. Lighthouse will check the color contrast of web pages, the availability of keyboard navigation, the clarity of form labels, etc.; it also includes Best Practices (BP). Lighthouse will check whether the web page uses HTTPS, whether there are security risks such as cross-site scripting, and whether it follows the latest web development standards and best practices; it also includes Search Engine Optimization (SEO). Lighthouse will evaluate the meta tags, link structure, content quality, etc. of web pages to determine the performance of web pages in search engines. Get a comprehensive analysis and evaluation report on web pages through Lighthouse.

[0060] S102: If the first content drawing time is greater than a first preset threshold, analyzing the dependency relationship between the language file and the cascading style sheet file.

[0061] In this embodiment, when the first content painting (FCP) time in the performance evaluation report is greater than the first preset threshold, it indicates that the time taken from the user starting to access the Web page to the browser first rendering the content (such as text, image) on the page exceeds the first preset threshold, and the user waits too long for the page content to appear. The dependency relationship between the language file (JavaScript) and the Cascading Style Sheets (CSS) file is deeply understood, where JavaScript is usually used to implement the interactive functions and dynamic effects of the page, and CSS is used to describe the style and layout of the page. There is a direct dependency between them. For example, a JavaScript function needs to wait for a CSS style to be loaded before it can be executed correctly. By analyzing the dependency relationship between the language file and the Cascading Style Sheet file, the JavaScript sub-files and CSS sub-files required for the initial loading of the Web page, as well as the JavaScript sub-files and CSS sub-files that can be loaded lazily, are determined.

[0062] S103: Split the language file and the cascading style sheet file into language sub-files and cascading style sheet sub-files according to the dependency relationship.

[0063] In this embodiment, after determining the file dependencies, JavaScript and CSS files with lower dependencies are screened out and split into smaller sub-files to reduce the size of a single file and increase the loading speed. At the same time, by splitting the files, the order and timing of file loading can be controlled more flexibly.

[0064] In one embodiment, splitting the language file and the cascading style sheet file according to the dependency relationship to obtain language sub-files and cascading style sheet sub-files includes: obtaining the dependency relationship between the language file and the cascading style sheet file; determining the splitting point between the language file and the cascading style sheet file according to the dependency relationship; and creating corresponding language sub-files and cascading style sheet sub-files according to the splitting point.

[0065] In this embodiment, tools such as AST parsers or manual code inspection are used to find out the references and calling relationships between all functions, classes, and modules, including variables, function parameters, module imports and exports, etc. Dynamic analysis is performed by recording function call stacks, object creation and other events at runtime to capture actual runtime dependencies.

[0066] On the other hand, by analyzing the CSS selector, it is determined that the style rule depends on a specific HTML element or class, and the parent element of the style is identified according to the inheritance rules of the CSS attributes. According to the split point determined by the dependencies captured above, a new language sub-file and cascading style sheet sub-file are created for each split code block or style rule, ensuring that the sub-files have clear naming and structure for subsequent management and maintenance, and moving the code or style in the original file to the corresponding sub-file. It should be noted that if there are cross-file references or dependencies, ensure that appropriate adjustments are made in the sub-files to maintain the integrity and correctness of the code. If other files reference the split code or style, ensure that these references are updated to point to the new sub-files.

[0067] S104: Setting the loading order of the language sub-file and the cascading style sheet sub-file by a front-end resource construction tool, wherein the language sub-file includes an initial language sub-file and a non-initial language sub-file, and the cascading style sheet sub-file includes an initial cascading style sheet sub-file and a non-initial cascading style sheet sub-file.

[0068] In this embodiment, the loading order of JavaScript sub-files and CSS sub-files is set by using the front-end resource building tool Webpack. According to the dependency relationship between the files and the actual needs of the page, the JavaScript sub-files and CSS sub-files required for initial loading are determined, such as the basic style and key interactive functions of the page required for initial loading, and the sub-files that can be loaded lazily, such as the style and functions of non-critical pages, are determined. By reasonably configuring the loading order, it is ensured that users can see the key content as soon as possible when accessing the page, while reducing unnecessary resource loading. At the same time, the language sub-files include the initial language sub-files and non-initial language sub-files, and the cascading style sheet sub-files include the initial cascading style sheet sub-files and non-initial cascading style sheet sub-files.

[0069] S105: Delay loading the non-initial language sub-file and the non-initial cascading style sheet sub-file according to the loading sequence.

[0070] In this embodiment, non-initial JavaScript sub-files and CSS sub-files are delayed in loading according to the loading order of JavaScript sub-files and CSS sub-files. After the initial loading of the page is completed, the corresponding sub-files are dynamically loaded according to the user's operations or the requirements of the page, thereby reducing the amount of resources during initial loading and improving the response speed of the page.

[0071] See also Figure 2 In one embodiment, the delayed loading of the non-initial language sub-file and the non-initial cascading style sheet sub-file by the loading sequence comprises steps S1051-S1052:

[0072] S1051: Delay loading of the non-initial language sub-file through dynamic import of the front-end resource construction tool.

[0073] In this embodiment, the sub-files that need to be delayed in loading are determined through the loading order, such as the style and functions of non-critical pages. In the JavaScript sub-file, the dynamic import (dynamicimport) supported by the front-end resource building tool Webpack is used to delay the loading of non-initial language sub-files and non-initial CSS sub-files. Dynamic import allows code to be loaded on demand at runtime.

[0074] S1052: Delay loading of the non-initial cascading style sheet sub-file by the cascading style sheet loader of the front-end resource construction tool.

[0075] In this embodiment, after the CSS file is split to obtain CSS sub-files, non-initial CSS sub-files are identified according to the function of each CSS sub-file, that is, it is determined that some CSS sub-files are not necessary for the initial loading of the page. On-demand loading is achieved by using the Cascading Style Sheet Loader CSS-Loader provided by the front-end resource building tool Webpack. CSS-Loade can inject non-initial CSS sub-files into the Document Object Model (DOM) at runtime, and realize dynamic loading control of Web page styles by modifying DOM elements.

[0076] S106: Load the initial language sub-file and the initial cascading style sheet sub-file in the loading sequence.

[0077] In this embodiment, according to the loading order, the CSS initial sub-file is loaded first, which defines the layout and style of the page, and the JavaScript initial sub-file is loaded after the CSS for interaction, animation and data operation. It should be noted that although some JavaScript codes need to be executed at the beginning of page loading (for example, for initializing certain components or settings), most JavaScript codes can be executed after the page content CSS sub-file is loaded to avoid blocking the rendering of the page.

[0078] In one embodiment, the initial language sub-file includes a picture to be loaded, and loading the initial language sub-file through the loading sequence includes: compressing the picture to be loaded into a network graphics format or a scalable vector graphics format through the front-end resource construction tool; and loading the compressed picture through a uniform resource locator loader or a file loader of the front-end resource construction tool.

[0079] In this embodiment, the loaded pictures are compressed by the network graphics format and scalable vector graphics format of the front-end resource construction tool, including the network graphics format (Web Picture, WebP), which is a modern image format that provides better compression than JPEG and PNG. The front-end resource construction tool can be configured to convert the picture into the WebP format to reduce the file size; it also includes the scalable vector graphics format (SVG), which is used for vector graphics and can reduce the file size by simplifying graphic paths and optimizing colors. During the construction process, the tool can use plug-ins or configuration options to automatically compress pictures to reduce bandwidth consumption and page loading time.

[0080] Furthermore, when the URL loader loads small files, such as images and fonts, the files are embedded in the generated JavaScript file as Base64-encoded URLs. For larger image files, use the file loader to move them to the output directory and reference them in the code through relative paths.

[0081] In one embodiment, the loading of the compressed image through the uniform resource locator loader or the file loader of the front-end resource construction tool includes: determining through the front-end resource construction tool whether the size of the compressed image is less than a third preset threshold; if the size of the image is less than the third preset threshold, configuring the uniform resource locator loader to convert the compressed image into Base64 encoding and embed it into the language file or the cascading style sheet file; if the size of the image is greater than the third preset threshold, configuring the file loader to output the compressed image to a designated directory.

[0082] In this embodiment, first, when loading a picture, the system will determine whether the size of the picture is less than a third preset threshold. If the size of the picture is less than the third preset threshold, the system will configure the uniform resource locator loader to convert the picture into Base64 encoding, convert the picture data into an ASCII string, and embed it into a language file (such as a JavaScript file) or a Cascading Style Sheet (CSS) file. The number of HTTP requests is reduced, and the picture is no longer loaded as an external file, but is directly included in the JavaScript or CSS file, which speeds up the loading speed of the page, especially when there are a large number of pictures or the network conditions are poor.

[0083] On the other hand, if the size of the image is larger than the third preset threshold, the system will configure the file loader to process the image. The file loader will output the image to the specified directory and reference the image in the code through a relative path or URL to maintain the original quality and format of the image, while avoiding the problem of excessive file size that may be caused by embedding large images into JavaScript or CSS files. By externally referencing images, the browser's cache mechanism can also be used to further improve the page loading performance. By selecting the appropriate processing method based on the image size, the front-end resource construction tool can flexibly load and manage image resources, which not only ensures the page loading speed, but also maintains the quality and availability of the image.

[0084] See also Figure 3 In one embodiment, after obtaining the first content drawing time through the web page analysis tool, the method further includes steps S301-S304:

[0085] S301: respectively obtaining the size of the language file and the size of the cascading style sheet file, and setting a corresponding fourth preset threshold and a fifth preset threshold.

[0086] In this embodiment, by reading and parsing the performance evaluation report, the size information of the language file (JavaScript file) and the cascading style sheet file (CSS file) is obtained respectively. In the performance evaluation report, the file information size is given in bytes or kilobytes. According to historical information and the performance requirements and optimization goals of the project, the corresponding fourth preset threshold and fifth preset threshold are set based on the upper limit of the file size, and the size data of the extracted language file and cascading style sheet file are stored in variables or data structures for subsequent search.

[0087] S302: If there is a language file whose size is larger than the fourth preset threshold or there is a cascading style sheet file whose size is larger than the fifth preset threshold, configure a code compression plug-in through the front-end resource construction tool.

[0088] In this embodiment, the size of the acquired language file is compared with the set fourth preset threshold. If the size of the language file exceeds the fourth preset threshold, it indicates that the file may be too large and needs to be optimized. The size of the cascading style sheet file is compared with the set fifth preset threshold. If the size of the cascading style sheet file exceeds the fifth preset threshold, it also indicates that optimization is needed. That is, as long as the size of any one of the language file or the cascading style sheet file exceeds the corresponding preset threshold, it is determined that code compression is required.

[0089] Furthermore, use the front-end resource building tool Webpack to configure the code compression plug-in. For JavaScript files, configure the plug-in to remove unused code, shorten variable names, delete comments, etc.; for CSS files, configure the plug-in to merge selectors, remove invalid attributes, shorten class names, etc.

[0090] S303: compressing the target language file or the target cascading style sheet file by using the code compression plug-in, and obfuscating the target language file or the target cascading style sheet file.

[0091] In this embodiment, after configuring the code compression plug-in, the execution of the code compression plug-in is triggered, and the plug-in will automatically scan the language files and cascading style sheet files in the project and perform compression operations. The compression plug-in reduces the size of the file by deleting unnecessary characters, optimizing variable names, etc. At the same time, it also retains the functionality of the code to ensure that the compressed file can run normally.

[0092] On the other hand, the code compression plug-in also improves security by renaming variables, functions and properties, changing the code structure, and obfuscating the code logic, making the code more difficult to read and understand. After the compression plug-in processes the language file and cascading style sheet file, it will generate a compressed file and save it to the specified directory.

[0093] In one embodiment, compressing the target language file or the target cascading style sheet file by the code compression plug-in includes: compressing the target language file by the code compression plug-in to remove useless code and comments; compressing the target cascading style sheet file by the code compression plug-in to remove redundant styles and invalid attributes; and automatically removing unused target language files or target cascading style sheet files by the code compression plug-in.

[0094] In this embodiment, the TerserPlugin is installed into the project through the package management tool, and the configuration of the TerserPlugin is added to the configuration file of the front-end resource building tool, including whether to retain comments and whether to obfuscate variable names. When the build command is run, TerserPlugin will automatically compress the language file, remove useless code and comments, and generate a compressed file.

[0095] Similarly, install the Cascading Style Sheet Nano plug-in through the package management tool. In the configuration of the build tool, add the Nano plug-in to the CSS processing flow. The Nano plug-in will compress the Cascading Style Sheet file, remove redundant styles and invalid attributes, and generate a compressed CSS file.

[0096] Furthermore, the front-end asset building tool Webpack configures the tree shaking optimization function. When running the build command, the front-end asset building tool will automatically analyze the code dependencies and remove the code that is not actually used, thereby reducing the size of the final package file.

[0097] The web page loading optimization method provided in this embodiment obtains the first content drawing time through a web page analysis tool; if the first content drawing time is greater than a first preset threshold, the dependency relationship between the language file and the cascading style sheet file is analyzed; the language file and the cascading style sheet file are split according to the dependency relationship to obtain a language sub-file and a cascading style sheet sub-file; the loading order of the language sub-file and the cascading style sheet sub-file is set through a front-end resource construction tool, the language sub-file includes an initial language sub-file and a non-initial language sub-file, and the cascading style sheet sub-file includes an initial cascading style sheet sub-file and a non-initial cascading style sheet sub-file; the non-initial language sub-file and the non-initial cascading style sheet sub-file are delayed to load through the loading order; the initial language sub-file and the initial cascading style sheet sub-file are loaded through the loading order. The language file and the cascading style sheet file are split through the front-end resource construction tool, and the sub-files required by the current page are loaded on demand, thereby reducing the initial loading time.

[0098] Example 2

[0099] In addition, an embodiment of the present application provides a web page loading optimization device, which is applied to an electronic device.

[0100] like Figure 4 As shown, the webpage loading optimization device 400 includes:

[0101] An acquisition module 401 is used to acquire the first content drawing time through a web page analysis tool;

[0102] The judgment module 402 is used to analyze the dependency relationship between the language file and the cascading style sheet file if the first content drawing time is greater than a first preset threshold;

[0103] A splitting module 403 is used to split the language file and the cascading style sheet file into language sub-files and cascading style sheet sub-files according to the dependency relationship;

[0104] A setting module 404 is used to set the loading order of the language sub-file and the cascading style sheet sub-file through a front-end resource construction tool, wherein the language sub-file includes an initial language sub-file and a non-initial language sub-file, and the cascading style sheet sub-file includes an initial cascading style sheet sub-file and a non-initial cascading style sheet sub-file;

[0105] A delay module 405, configured to delay loading of the non-initial language sub-file and the non-initial cascading style sheet sub-file according to the loading sequence;

[0106] The loading module 406 is used to load the initial language sub-file and the initial cascading style sheet sub-file according to the loading sequence.

[0107] Optionally, the splitting module 403 is further used to obtain the dependency relationship between the language file and the cascading style sheet file; determine the splitting point between the language file and the cascading style sheet file according to the dependency relationship; and create corresponding language sub-files and cascading style sheet sub-files according to the splitting point.

[0108] Optionally, the delay module 405 is further used to delay the loading of the non-initial language sub-file through dynamic import of the front-end resource construction tool; and delay the loading of the non-initial cascading style sheet sub-file through the cascading style sheet loader of the front-end resource construction tool.

[0109] Optionally, the loading module 406 is also used to compress the image to be loaded into a network graphics format or a scalable vector graphics format through the front-end resource construction tool; and load the compressed image through the uniform resource locator loader or file loader of the front-end resource construction tool.

[0110] Optionally, the loading module 406 is also used to determine whether the size of the compressed image determined by the front-end resource construction tool is less than a third preset threshold; if the size of the image is less than the third preset threshold, configure the uniform resource locator loader to convert the compressed image into Base64 encoding and embed it into the language file or the cascading style sheet file; if the size of the image is greater than the third preset threshold, configure the file loader to output the compressed image to a designated directory.

[0111] Optionally, the web page loading optimization device 400 shown also includes a compression module, which is used to obtain the size of the language file and the size of the cascading style sheet file respectively, and set the corresponding fourth preset threshold and fifth preset threshold; if the size of the language file is greater than the fourth preset threshold or the size of the cascading style sheet file is greater than the fifth preset threshold, configure a code compression plug-in through the front-end resource construction tool; compress the target language file or the target cascading style sheet file through the code compression plug-in, and obfuscate the target language file or the target cascading style sheet file.

[0112] Optionally, the compression module is also used to compress the target language file through the code compression plug-in to remove useless code and comments; compress the target cascading style sheet file through the code compression plug-in to remove redundant styles and invalid attributes; and automatically remove unused target language files or target cascading style sheet files through the code compression plug-in.

[0113] The web page loading optimization device 400 provided in this embodiment can implement the web page loading optimization method provided in Embodiment 1, and will not be described again here to avoid repetition.

[0114] The webpage loading optimization device provided in this embodiment obtains the first content drawing time through a webpage analysis tool; if the first content drawing time is greater than a first preset threshold, the dependency relationship between the language file and the cascading style sheet file is analyzed; the language file and the cascading style sheet file are split according to the dependency relationship to obtain a language sub-file and a cascading style sheet sub-file; the loading order of the language sub-file and the cascading style sheet sub-file is set through a front-end resource construction tool, the language sub-file includes an initial language sub-file and a non-initial language sub-file, and the cascading style sheet sub-file includes an initial cascading style sheet sub-file and a non-initial cascading style sheet sub-file; the non-initial language sub-file and the non-initial cascading style sheet sub-file are delayed to load through the loading order; the initial language sub-file and the initial cascading style sheet sub-file are loaded through the loading order. The language file and the cascading style sheet file are split through the front-end resource construction tool, and the sub-files required by the current page are loaded on demand, thereby reducing the initial loading time.

[0115] Example 3

[0116] In addition, an embodiment of the present application provides an electronic device, including a memory and a processor, wherein the memory stores a computer program, and when the computer program runs on the processor, the web page loading optimization method provided in Embodiment 1 is executed.

[0117] The electronic device provided in the embodiment of the present invention can execute the steps of the web page loading optimization method provided in the above method embodiment 1, which will not be described again here to avoid repetition.

[0118] Example 4

[0119] The present application also provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the web page loading optimization method provided in Example 1 is implemented.

[0120] In this embodiment, the computer-readable storage medium may be a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk.

[0121] The computer-readable storage medium provided in this embodiment can implement the webpage loading optimization method provided in Embodiment 1, and will not be described again here to avoid repetition.

[0122] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or terminal including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or terminal. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the presence of other identical elements in the process, method, article or terminal including the element.

[0123] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, a magnetic disk, or an optical disk), and includes a number of instructions for a terminal (which can be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.) to execute the methods described in each embodiment of the present application.

[0124] The embodiments of the present application are described above in conjunction with the accompanying drawings, but the present application is not limited to the above-mentioned specific implementation methods. The above-mentioned specific implementation methods are merely illustrative and not restrictive. Under the guidance of the present application, ordinary technicians in this field can also make many forms without departing from the purpose of the present application and the scope of protection of the claims, all of which are within the protection of the present application.

Claims

1. A web page loading optimization method, characterized in that: The method comprises: Get the first contentful paint time through web analytics tools; If the first content drawing time is greater than a first preset threshold, analyzing the dependency relationship between the language file and the cascading style sheet file; Splitting the language file and the cascading style sheet file according to the dependency relationship to obtain a language sub-file and a cascading style sheet sub-file; Setting the loading order of the language sub-file and the cascading style sheet sub-file by a front-end resource construction tool, wherein the language sub-file includes an initial language sub-file and a non-initial language sub-file, and the cascading style sheet sub-file includes an initial cascading style sheet sub-file and a non-initial cascading style sheet sub-file; Delay loading of the non-initial language sub-file and the non-initial cascading style sheet sub-file by the loading sequence; The initial language sub-file and the initial cascading style sheet sub-file are loaded in the loading sequence.

2. The method according to claim 1, characterized in that The step of splitting the language file and the cascading style sheet file according to the dependency relationship to obtain a language sub-file and a cascading style sheet sub-file comprises: Obtaining the dependency relationship between the language file and the cascading style sheet file; Determining a split point between the language file and the cascading style sheet file according to the dependency relationship; According to the split point, a corresponding language sub-file and cascading style sheet sub-file are created.

3. The method according to claim 1, characterized in that The delaying of loading the non-initial language sub-file and the non-initial cascading style sheet sub-file by the loading sequence includes: Delayed loading of the non-initial language sub-file by dynamic import of the front-end resource construction tool; The non-initial cascading style sheet sub-file is lazily loaded by the cascading style sheet loader of the front-end resource construction tool.

4. The method according to claim 1, characterized in that: The initial language sub-file includes a picture to be loaded, and loading the initial language sub-file according to the loading sequence includes: Compressing the to-be-loaded picture into a network graphics format or a scalable vector graphics format by the front-end resource construction tool; The compressed image is loaded through the uniform resource locator loader or the file loader of the front-end resource construction tool.

5. The method according to claim 4, characterized in that The step of loading the compressed image through the uniform resource locator loader or the file loader of the front-end resource construction tool includes: Determining, by the front-end resource construction tool, whether the size of the compressed image is less than a third preset threshold; If the size of the picture is smaller than a third preset threshold, configuring the uniform resource locator loader to convert the compressed picture into Base64 encoding and embed it into the language file or the cascading style sheet file; If the size of the image is greater than a third preset threshold, the file loader is configured to output the compressed image to a specified directory.

6. The method according to claim 1, characterized in that After obtaining the first content drawing time through the webpage analysis tool, the method further includes: Respectively obtaining the size of the language file and the size of the cascading style sheet file, and setting a corresponding fourth preset threshold and a fifth preset threshold; If there is a language file whose size is larger than the fourth preset threshold or there is a cascading style sheet file whose size is larger than the fifth preset threshold, configuring a code compression plug-in through the front-end resource construction tool; The target language file or the target cascading style sheet file is compressed by the code compression plug-in, and the target language file or the target cascading style sheet file is obfuscated.

7. The method according to claim 6, characterized in that The step of compressing the target language file or the target cascading style sheet file by using the code compression plug-in comprises: Compressing the target language file by using the code compression plug-in to remove useless codes and comments; Compressing the target cascading style sheet file by using the code compression plug-in to remove redundant styles and invalid attributes; The unused target language files or target cascading style sheet files are automatically removed by the code compression plug-in.

8. A webpage loading optimization device, characterized in that: The device comprises: The acquisition module is used to obtain the first content painting time through the web page analysis tool; A judgment module, configured to analyze the dependency relationship between the language file and the cascading style sheet file if the first content drawing time is greater than a first preset threshold; A splitting module, used for splitting the language file and the cascading style sheet file into language sub-files and cascading style sheet sub-files according to the dependency relationship; A setting module, used for setting the loading order of the language sub-file and the cascading style sheet sub-file through a front-end resource construction tool, the language sub-file includes an initial language sub-file and a non-initial language sub-file, and the cascading style sheet sub-file includes an initial cascading style sheet sub-file and a non-initial cascading style sheet sub-file; A delay module, used for delaying the loading of the non-initial language sub-file and the non-initial cascading style sheet sub-file according to the loading sequence; A loading module is used to load the initial language sub-file and the initial cascading style sheet sub-file according to the loading sequence.

9. An electronic device, characterized in that: The method comprises a memory and a processor, wherein the memory stores a computer program, and the computer program executes the web page loading optimization method according to any one of claims 1 to 7 when the processor runs.

10. A computer-readable storage medium, characterized in that: It stores a computer program, which executes the web page loading optimization method described in any one of claims 1 to 7 when running on a processor.

Citation Information

Patent Citations

  • Method and device for optimizing webpage pre-loading

    CN105701113A

  • Page loading method, device and apparatus, and storage medium

    CN109359256A

  • JS file loading method and system of front-end webpage and storage medium

    CN116992177A

  • Webpage loading method and device and readable storage medium

    CN117235398A

Cited By

  • Style storage method and device, equipment and storage medium

    CN121144561A

  • A style storage method, device, equipment and storage medium

    CN121144561B

  • CSS dependency optimization method and device, equipment and storage medium

    CN121257683A