Localization method and system of network resources and electronic equipment
By obtaining and downloading network resources in front-end files and writing them to the project directory, the problem of inefficient localization of network resources in the existing technology is solved, and rapid response and resource access is achieved in the case of network disconnection.
Patent Information
- Application Number
- CN202311102534.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-08-29
- Publication Date
- 2025-02-28
AI Technical Summary
The prior art is inefficient when localizing network resources and cannot access network resources without connecting to the network.
By obtaining the corresponding files of the source code, identifying the corresponding network resources in the front-end file, and downloading these resources to the server through text files, and writing them to the preset project directory. When receiving a network request, obtain the corresponding network resources from the project directory.
It realizes rapid response to user requests when the network cannot be connected, and returns the required network resources, improving the efficiency and reliability of network resource localization.
Smart Images

Figure CN119544696A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of data processing technology, and in particular to a method, system and electronic device for localizing network resources. Background Art
[0002] When accessing some web pages or applications, in order to respond quickly, the web pages or applications will distribute network resources such as images, files, fonts, etc. through the Content Delivery Network (CDN). However, for some special institutions, such as government agencies and schools, they can only access the internal LAN and cannot access the external network, and thus cannot access the CDN network resources. Therefore, it is necessary to localize the network resources of the web pages or applications.
[0003] In the current technical solution, in the process of localizing network resources, considering the version issue, it is necessary to create a new project or a new branch to process the version of the localized web page or application, and then manually replace the network resources with the resources that have been loaded locally. This method is not only inefficient, but also the local resources cannot be changed once replaced, and when the network cannot be connected, access to network resources will fail. Summary of the invention
[0004] In order to solve the above technical problems, the embodiments of the present application provide a method, system and electronic device for localizing network resources to solve the problem of failure to access network resources when the network cannot be connected.
[0005] The embodiments of the present application provide the following technical solutions:
[0006] In a first aspect, an embodiment of the present application provides a method for localizing network resources, which is applied to an electronic device, including:
[0007] Obtain files corresponding to the source code, wherein the files corresponding to the source code include front-end files and text files;
[0008] If the front-end file meets the limiting conditions, the corresponding network resource in the front-end file is identified;
[0009] Download network resources from the server through text files and write them into the preset project directory;
[0010] After receiving the network request, the network resources corresponding to the network request are obtained from the project directory according to the network request.
[0011] In some embodiments, before obtaining the file corresponding to the source code, the process includes:
[0012] Create a project, which includes files corresponding to the source code and a preset project directory. The preset project directory is used to store network resources corresponding to network requests.
[0013] The file name and / or file path of the file corresponding to the source code is limited to obtain a limited condition, wherein the limited condition includes a limited file name and / or a limited file path.
[0014] In some embodiments, identifying a corresponding network resource in a front-end file includes:
[0015] Convert the source code into an abstract syntax tree, where the abstract syntax tree includes a plurality of nodes, where the nodes store a string literal of a first resource address, where the first resource address is a network resource address;
[0016] According to the network resource address, identify the corresponding network resource in the front-end file.
[0017] In some embodiments, after writing the network resource to the preset project directory, the following steps are included:
[0018] Mark the address of the network resource written into the preset project directory as the second resource address, and the second resource address corresponds to the first resource address one by one;
[0019] Modify the first resource address corresponding to the string literal of the node of the abstract syntax tree to the second resource address, and obtain a modified abstract syntax tree;
[0020] The modified abstract syntax tree is converted into a modified source code, and the modified source code is written into a file corresponding to the source code, the project is updated, an updated project is obtained, and the updated project is deployed.
[0021] In some embodiments, the electronic device includes a browser, the browser including a browser cache;
[0022] After receiving the network request, the network resources corresponding to the network request are obtained from the project directory according to the network request, including:
[0023] According to the network request, the network resources corresponding to the network request in the cache project directory are stored in the browser cache to obtain the cached resources;
[0024] Get cached resources from the browser cache.
[0025] In some embodiments, the method further comprises:
[0026] After receiving a network request for the same resource, the network resource corresponding to the network request is obtained according to the cache strategy, where the cache strategy includes cache time and network status, and the network status includes normal status and abnormal status;
[0027] If the network condition is normal, obtain the network resources corresponding to the network request according to the cache time;
[0028] If the network condition is abnormal and the cached resource corresponding to the network request exists in the browser cache, the cached resource corresponding to the network request is directly obtained.
[0029] In some embodiments, obtaining a network resource corresponding to a network request according to a cache time includes:
[0030] If the cache time is within the threshold range, search whether the network resource corresponding to the network request exists in the browser cache, obtain the detection result, and obtain the network resource corresponding to the network request according to the detection result;
[0031] If the cache time is not within the threshold range, the network resource corresponding to the network request is obtained from the server again.
[0032] In some embodiments, the detection result includes a first result and a second result, and obtaining a network resource corresponding to the network request according to the detection result includes:
[0033] If the detection result is the first result, directly obtain the cache resource corresponding to the network request from the browser cache, and the first result is that the network resource corresponding to the network request exists in the browser cache;
[0034] If the detection result is the second result, the network resource corresponding to the network request is obtained from the server again, and the second result is that the network resource corresponding to the network request does not exist in the browser cache.
[0035] In a second aspect, an embodiment of the present application provides an electronic device, including:
[0036] At least one processor and at least one memory, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor executes the network resource localization method of the first aspect.
[0037] In a third aspect, an embodiment of the present application provides a network resource localization system, including:
[0038] The electronic device as in the second aspect;
[0039] The local server is used to download the corresponding network resources in the front-end file from the external server, write the downloaded network resources to the preset project directory, and cache the network resources to the browser cache;
[0040] The external server is used to provide the local server with network resources corresponding to the network request.
[0041] The beneficial effects of the implementation methods of the present application are as follows: different from the prior art, the implementation methods of the present application provide a method for localizing network resources, the method comprising: obtaining files corresponding to source code, the files corresponding to source code including front-end files and text files; if the front-end file meets the specified conditions, identifying the corresponding network resources in the front-end file; downloading the corresponding network resources in the front-end file from the server through the text file, and writing the network resources into a preset project directory; after receiving the network request, obtaining the network resources corresponding to the network request from the project directory according to the network request, and by downloading the network resources to the project directory, the present application can quickly respond to the user's request and return the network resources required by the user. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] One or more embodiments are exemplarily described by corresponding drawings, which do not constitute limitations on the embodiments. Elements with the same reference numerals in the drawings represent similar elements, and the figures in the drawings do not constitute proportional limitations unless otherwise stated.
[0043] Figure 1 is a schematic diagram of an application environment provided by an embodiment of the present application;
[0044] Figure 2 It is a schematic diagram of a flow chart of setting limiting conditions provided in an embodiment of the present application;
[0045] Figure 3 It is a flowchart of a method for localizing network resources provided in an embodiment of the present application;
[0046] Figure 4 yes Figure 3 A detailed flow chart of step S303 in FIG.
[0047] Figure 5 It is a flowchart of a deployment project provided by an embodiment of the present application;
[0048] Figure 6 yes Figure 3 A detailed flow chart of step S306 in FIG.
[0049] Figure 7 This is a flowchart of obtaining cache resources provided by this application;
[0050] Figure 8 yes Figure 7 A detailed flow chart of step S703 in FIG.
[0051] Fig. 9 yes Figure 8 A detailed flow chart of step S733 in FIG.
[0052] Fig.10It is a structural diagram of a network resource localization system provided in an embodiment of the present application;
[0053] Fig.11 It is a timing diagram of obtaining network resources corresponding to a network request provided by this application;
[0054] Fig.12 It is a structural diagram of a network resource localization device provided in an embodiment of the present application;
[0055] Fig.13 It is a structural schematic diagram of an electronic device provided in an embodiment of the present application.
[0056] Description of Figure Numbers:
[0057]
[0058] DETAILED DESCRIPTION
[0059] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0060] It should be noted that, if there is no conflict, the various features in the embodiments of the present application can be combined with each other, and all are within the scope of protection of the present application. In addition, the words "first", "second", etc. used in the present application do not limit the data, but only divide the same or similar items with basically the same functions and effects.
[0061] The technical solution of the present application is described in detail below in conjunction with the accompanying drawings:
[0062] See also Figure 1 , Figure 1 is a schematic diagram of an application environment provided by an embodiment of the present application;
[0063] like Figure 1As shown, the application environment 100 includes: an electronic device 10, a local server 20, and an external server 30, wherein the electronic device 10 is used to initiate a network resource request to the local server 20 and save the network resources corresponding to the network resource request downloaded from the local server 20, the local server 20 is used to download and save the network resources in the external server 30, the electronic device 10 is connected to the local server 20 through a wireless network or a wired interface, and the local server 20 is connected to the external server 30 through a wireless network. It can be understood that the wired interface includes interfaces such as SerialPort, USB, Ethernet, etc., and the wireless network includes wireless networks such as Bluetooth, wireless LAN, and mobile network.
[0064] In the embodiment of the present application, the electronic device 10 includes a tablet computer, a computer terminal, or other network-connected device or apparatus, the local server 20 is a server deployed in a local computer or a local area network, such as a Web server, and the external server 30 is a server hosted on the Internet, such as a cloud server or a CDN server. The local server 20 provides a communication interface for the electronic device 10 so that the electronic device 10 can download network resources, and the external server 30 provides a communication interface for the local server 20 so that the local server 20 can obtain network resources in the external server 30.
[0065] See also Figure 2 , Figure 2 It is a schematic diagram of a flow chart of setting limiting conditions provided in an embodiment of the present application;
[0066] like Figure 2 As shown in FIG. 1 , the process of setting limiting conditions includes:
[0067] Step S201: creating a project, the project including files corresponding to source code and a preset project directory, the preset project directory is used to store network resources corresponding to network requests;
[0068] In the embodiment of the present application, each project corresponds to an application or public web page that needs to be localized.
[0069] Specifically, a project is created on a local terminal, files corresponding to the source code are written in the project, and a project directory is created, wherein the project can initiate a network resource download request to the server and download the network resources corresponding to the network resource download request. Files corresponding to the source code include front-end files and text files. The project directory is used to save resource files in the project and network resources corresponding to the network request, such as pictures, videos, etc. The project directory is the preset project directory.
[0070] In an embodiment of the present application, the project directory includes a resource folder, and the resource folder includes an assets folder. The resource files in the project and the network resources corresponding to the network request are stored in the assets folder. The front-end file includes an HTML file, and the local browser can obtain the address of the network resource by parsing the HTML file. The text file includes a serviceworker (sw) file, and the serviceworker file is actually a Javascrip (JS) file. Among them, serviceworker is equivalent to a proxy tool, and the proxy tool is used to handle the interactive actions between the Web application and the network, that is, it acts as a proxy server between the Web application, the browser and the network server. For example: intercept and process network requests, and take appropriate actions, update resources from the server, cache files for offline use, etc. according to whether the network is available. For example: when the user opens the Web interface, the proxy tool will also run. When the browser parses the HTML file to obtain the network resource address in the HTML file in the CDN server, the proxy tool initiates a network request to the CDN server. For example: switch to the browser page on the electronic device and click F12. Switching the network option can observe the network request initiated to the CDN server. The proxy tool downloads the network resources corresponding to the network request to the project directory.
[0071] Step S202: limiting the file name and / or file path of the file corresponding to the source code to obtain limiting conditions, where the limiting conditions include the limited file name and / or the limited file path;
[0072] Specifically, when creating a project, the file name or folder corresponding to the source code is limited to obtain a limited file name and / or a limited file path, and the source code is limited to be saved only in the file corresponding to the limited file name and / or file path.
[0073] In an embodiment of the present application, when creating a serviceworker file, it is necessary to limit the scope of the serviceworker and use the method in the first function to set a scope parameter, such as the register method in the ServiceWorkerContainer function. The parameter can be a URL or a file path. The URL or file path specifies that the serviceworker only acts on the file name or folder corresponding to the limited source code.
[0074] In the embodiment of the present application, by limiting the folder or file name of the source code, the scope of the source code can be better defined.
[0075] See also Figure 3 , Figure 3It is a flowchart of a method for localizing network resources provided in an embodiment of the present application;
[0076] like Figure 3 As shown, the process of the network resource localization method includes:
[0077] Step S301: Obtain files corresponding to source code, where the files corresponding to source code include front-end files and text files;
[0078] Specifically, a text file and a front-end file of a web page to be localized are created in the project, and the front-end file and the text file are saved in a defined folder.
[0079] Step S302: Determine whether the front-end file meets the specified conditions;
[0080] Specifically, obtain the front-end file of the web page that needs to be localized, identify whether the file name of the front-end file conforms to the file name specified by the project, or whether the path where the front-end file is stored conforms to the file path specified by the project, if so, jump to step S303, if not, jump to step S306.
[0081] Step S303: Identify the corresponding network resources in the front-end file;
[0082] Specifically, when the front-end file of the web page that needs to be localized meets the file name specified by the project, or the storage path of the front-end file of the web page that needs to be localized meets the file path specified by the project, the front-end files of the web page that needs to be localized are compiled one by one, and the address of the network resource in the front-end file of the web page that needs to be localized is obtained, and the corresponding network resource in the front-end file of the web page that needs to be localized is found through the address.
[0083] Please refer to Figure 4 , Figure 4 yes Figure 3 A detailed flow chart of step S303 in FIG.
[0084] like Figure 4 As shown, step S303 includes:
[0085] Step S331: converting the source code into an abstract syntax tree, the abstract syntax tree including a plurality of nodes, the nodes storing a string literal of a first resource address, the first resource address being a network resource address;
[0086] In an embodiment of the present application, an abstract syntax tree (AST) is a tree representation of an abstract syntax structure, which can represent the structure and syntax of a text written in a formal language and is mainly used for code conversion. For example, if the text written in a formal language is source code, the source code can be converted into a tree form using an abstract syntax tree, and the structure and syntax of the source code can be represented in the form of a tree.
[0087] In an embodiment of the present application, the abstract syntax tree includes multiple nodes, which can be used to store syntax structure types, such as expressions, binary operators, etc. The nodes can also contain some attributes, such as operands, results, etc.
[0088] In the embodiment of the present application, the string literal corresponding to the network resource address is a text data type that can be directly written into the node of the abstract syntax tree. The value of the string literal corresponds to the network resource address. It is understandable that the value of the string literal refers to the character sequence between two double quotes or single quotes, for example: "string", "literal", '12345'.
[0089] Specifically, compile the source code in the file corresponding to the source code of the web page to be localized, and use a source code conversion tool to convert the source code. For example, the jscodeshift-npm tool can convert the source code into an abstract syntax tree to obtain an abstract syntax tree after the source code conversion. There are multiple nodes on the abstract syntax tree. The nodes of the abstract syntax tree store string literals corresponding to network resource addresses. The value of the string literal is the network resource address in an external server, and the network resource address is the first resource address.
[0090] Step S332: Identify the corresponding network resource in the front-end file according to the network resource address;
[0091] Specifically, the network resource address in the external server is obtained. Each network resource address has a corresponding network resource. Through the network resource address, the network resource in the front-end file corresponding to the localized web page is found, and the network resource is downloaded to the local server.
[0092] Step S304: Failure in identifying the corresponding network resource in the front-end file;
[0093] Specifically, when the front-end file of the web page that needs to be localized does not conform to the file name specified by the project, or the storage path of the front-end file of the web page that needs to be localized does not conform to the file path specified by the project, access to the front-end file fails, and the corresponding network resources in the front-end file cannot be identified.
[0094] Step S305: downloading network resources from the server through the text file, and writing the network resources into a preset project directory;
[0095] In the embodiment of the present application, the text file includes a serviceworker (sw) file, which is actually a JavaScript (JS) file. The serviceworker file is equivalent to a proxy tool, which is used to process the interaction between the external server and the network.
[0096] In an embodiment of the present application, the network resources downloaded to the server are the network resources corresponding to the front-end files, wherein each front-end file includes at least one network resource address, and each network resource address corresponds one-to-one to a network resource.
[0097] Specifically, after converting the source code corresponding to the web page to be localized into an abstract syntax tree, the abstract syntax tree is accessed through a proxy tool to obtain the network resource address in the external server corresponding to the value of the node of the abstract syntax tree. According to the network resource address, the proxy tool is called to download the network resource corresponding to the network resource address, and the network resource is saved in the project directory in the project.
[0098] In the embodiment of the present application, the localization of the network resources is further achieved by downloading the network resources into the project directory of the local project.
[0099] Please refer to Figure 5 , Figure 5 It is a flowchart of a deployment project provided by an embodiment of the present application;
[0100] like Figure 5 As shown in the figure, the process of deploying the project includes:
[0101] Step S501: marking the address of the network resource written into the preset project directory as a second resource address, the second resource address corresponding to the first resource address one by one;
[0102] Specifically, after downloading the network resource to the project directory of the local project, mark the address of the network resource saved in the project directory and record the address as the second resource address, and the second resource address is the same as the network resource saved by the first resource address, that is, the second resource address and the first resource address correspond to each other. While keeping the web page content unchanged, to replace the network resource address, you only need to replace the second resource address corresponding to the address of the network resource.
[0103] Step S502: modify the first resource address corresponding to the string literal of the node of the abstract syntax tree to the second resource address, and obtain a modified abstract syntax tree;
[0104] Specifically, a proxy tool is used to search for the network resource address corresponding to the value of the string literal of the node of the abstract syntax tree and the second resource address in the project directory corresponding to the network resource address, and the network resource address corresponding to the value of the string literal of the node of the abstract syntax tree is modified to the second resource address in the project directory corresponding to the network resource address, so as to obtain the abstract syntax tree after the address is replaced, that is, the modified abstract syntax tree.
[0105] In an embodiment of the present application, the abstract syntax tree is modified to obtain an abstract syntax tree after the address is replaced. For example, the address of a picture tag in an HTML file is modified, and the source code of the HTML file is converted into its corresponding abstract syntax tree using the jscodeshift-pm tool. The string literal value of the node corresponding to the address of the picture tag is found through the proxy tool, and the string literal value of the node is modified to the address in the project directory corresponding to the picture. For example, the address before modification is http: / / xxx / abc.png, and the address after modification is the address in the project directory of the project, for example, .. / assets / abc.png after modification.
[0106] In an embodiment of the present application, the network resource address is modified by modifying the value of the string literal of the node of the abstract syntax tree, thereby localizing the network resource.
[0107] Step S503: converting the modified abstract syntax tree into a modified source code, writing the modified source code into a file corresponding to the source code, updating the project, obtaining an updated project, and deploying the updated project;
[0108] Specifically, use an abstract syntax tree conversion tool to convert the modified abstract syntax tree into a modified source code. For example, use the toSource() method in the jscodeshift-pm tool to convert the abstract syntax tree into the modified source code. Write the modified source code into the file corresponding to the source code, that is, overwrite the source code in the original file with the modified source code. For example, use the node.js file module to write the modified source code into the file corresponding to the source code to overwrite the source code in the file corresponding to the source code, or use the extensions function in the above-mentioned jscodeshift-pm tool to specify the file name of the overwritten file, find the file corresponding to the file name, and overwrite the source code in the file with the modified source code. After completing the above steps, update the project appropriately and install the updated project into the electronic device.
[0109] In an embodiment of the present application, by creating a project, the network resources on the web page are downloaded to the local project, and then the network resource address on the corresponding web page is modified to the address of the network resource in the local project to save the network resource to the local server.
[0110] Step S306: after receiving the network request, obtaining the network resource corresponding to the network request from the project directory according to the network request;
[0111] In an embodiment of the present application, a project corresponding to an application or public web page that needs to be localized is installed in an electronic device, a browser in the electronic device is opened, and the localized application or public web page is accessed through the browser. When the localized application is accessed for the first time, the proxy tool in the project starts to be installed and activated. The proxy tool intercepts the network request initiated by the user, and based on the network request, finds the network resource corresponding to the network request, and returns the corresponding page to the browser.
[0112] Specifically, the proxy tool receives a network request initiated by a user, and according to the network request of the user, traverses the network resources stored in a preset project directory in the project, and obtains the network resources corresponding to the network request from the project directory.
[0113] Please refer to Figure 6 , Figure 6 yes Figure 3 A detailed flow chart of step S306 in FIG.
[0114] As shown in Figure 6, step S306 includes:
[0115] Step S361: according to the network request, cache the network resources corresponding to the network request in the project directory to the browser cache to obtain the cached resources;
[0116] In an embodiment of the present application, the network resources saved in the project are localized applications or network resources on a public web page. Usually, a network request is initiated to a server through a browser on an electronic device to further obtain the localized application or network resources on a public web page.
[0117] In the embodiment of the present application, in order to quickly respond to the user's network request, it is necessary to cache the network resources of the network request to the browser cache provided by the browser, for example: the browser cache is CacheStorage, and the browser engine then obtains the resources corresponding to the network request from the browser cache and returns them to the browser page. The browser cache is used to organize and manage cache data.
[0118] Specifically, the proxy tool receives a network request initiated by the user in the browser, and the proxy tool traverses the network resources in the project directory, finds the network resources in the project directory corresponding to the network request, and then caches the network resources in the project directory corresponding to the network request into the browser cache. The network resources stored in the browser cache are cached data.
[0119] Step S362: Obtain cache resources from the browser cache;
[0120] Specifically, after the proxy tool caches the network resource corresponding to the network request into the browser cache, the browser detects that the network resource corresponding to the network request exists in the browser cache, and directly obtains the network resource corresponding to the network request from the browser cache.
[0121] In the embodiment of the present application, by caching network resources in the browser cache and directly obtaining the cached resources from the browser cache, it is possible to quickly respond to network requests initiated by users.
[0122] Please refer to Figure 7 , Figure 7 This is a flowchart of obtaining cache resources provided by this application;
[0123] like Figure 7 As shown in the figure, the process of obtaining cache resources includes:
[0124] Step S701: after receiving a network request for the same resource, obtaining the network resource corresponding to the network request according to a cache strategy, the cache strategy including cache time and network status, the network status including normal status and abnormal status;
[0125] In an embodiment of the present application, the proxy tool saves the address of the network resource corresponding to each intercepted network request in a database in the form of a key, so that subsequent requests can be compared and matched. For example, when the proxy tool intercepts a network request, it searches the database for records to see if there is an address in the database that is the same as the address of the current network request. If they are the same, it means that the network resource corresponding to the current network request has been processed, and the processed network resource is the same resource.
[0126] Specifically, when the browser initiates a network request for the same resource to the local server, the proxy tool intercepts the network request and obtains the network resource corresponding to the network request according to the cache strategy, that is, according to the network status and cache time, obtains the network resource corresponding to the network request. The network status is divided into normal status and abnormal status.
[0127] In the embodiment of the present application, the normal state represents a state in which the network connection is reliable, stable and working normally. In the normal state, network devices can communicate normally and data transmission and exchange are not interrupted or faulty; the abnormal state is divided into a weak network state and a disconnected network state. In these two cases, data cannot be transmitted between network devices and access to websites or other network activities are affected.
[0128] Step S702: Determine whether the network status is normal;
[0129] In the embodiments of the present application, the network connection can be tested in the electronic device to see whether it can access a specific website or service, or to test whether the network speed is stable and meets the requirements, or a network detection tool can be used to detect the network status and observe whether the indicator light in the detection tool is abnormal or not lit. The network status can also be detected in other ways, which will not be described in detail here.
[0130] Specifically, whether the network status is normal is detected in the above manner, if so, jump to step S703, if not, jump to step S704.
[0131] Step S703: Obtain the network resource corresponding to the network request according to the cache time;
[0132] In an embodiment of the present application, the cache time is the cache validity period of the cached resources in the browser cache, which is determined specifically according to the characteristics of the resources and the needs of the user. For example, a longer cache time can be set for static resources that do not change frequently, such as pictures, style sheets, scripts, etc. The cache time can be set to 7 days or more. For dynamic content or frequently updated data, a shorter cache time needs to be set, such as 1 day.
[0133] Specifically, when the network condition is normal, the network resource corresponding to the network request for the same resource initiated by the user is obtained according to the cache time, that is, according to the cache validity period.
[0134] Please refer to Figure 8 , Figure 8 yes Figure 7 A detailed flow chart of step S703 in FIG.
[0135] like Figure 8 As shown, step S703 includes:
[0136] Step S731: Obtain the network resource corresponding to the network request according to the cache time;
[0137] Specifically, the network resource corresponding to the network request for the same resource is obtained according to the cache time, that is, according to the limited period of the cache.
[0138] Step S732: determine whether the cache time is within a threshold range;
[0139] In an embodiment of the present application, a threshold range is set according to the characteristics of the resources required by the user. For example, if the storage time of the image in the browser cache is set to 7 days, the threshold range is 7 days.
[0140] In the embodiment of the present application, determining whether the cache time is within a threshold range is actually determining whether the time interval between a user initiating a network request for the same resource and the first time a network request is initiated for the same resource is within a threshold range. For example, if the threshold range is one day, then determining whether the time interval between a user initiating a network request for the same resource and the first time a network request is initiated for the same resource is more than one day.
[0141] Specifically, determine whether the time interval between the user initiating a network request for the same resource again and the first time the user initiated a network request for the same resource is within a threshold range. If so, jump to step S733; if not, jump to S734.
[0142] Step S733: searching whether the network resource corresponding to the network request exists in the browser cache, obtaining a detection result, and acquiring the network resource corresponding to the network request according to the detection result;
[0143] Specifically, when the cache time of the network resource corresponding to the network request is within the cache validity period, that is, the time interval between when the user initiates a network request for the same resource again and when the user initiates the network request for the first time is within the cache validity period, then it is detected whether the network resource corresponding to the network request for the same resource exists in the browser cache, and a detection result is obtained. According to the detection result, the network resource corresponding to the network request is obtained. For example, the network resource corresponding to the network request for the same resource initiated by the current user is a picture. Assuming that the cache validity period of the picture is 1 day, and the time interval between when the user initiates a network request for the same resource again and when the user initiates the network request for the same resource for the first time is less than 1 day, then it is continued to detect whether the browser cache contains the picture requested by the user.
[0144] Please refer to Fig. 9 , Fig. 9 yes Figure 8 A detailed flow chart of step S733 in FIG.
[0145] like Fig. 9 As shown, step S733 includes:
[0146] Step S7301: Determine the test result;
[0147] Specifically, detect whether the network resource corresponding to the network request for the same resource exists in the browser cache, and obtain a detection result, which includes a first result and a second result. The first result is that the network resource corresponding to the network request exists in the browser cache, and the second result is that the network resource corresponding to the network request does not exist in the browser cache. When the detection result is the first result, jump to step S7302, and when the detection result is the second result, jump to step S7303.
[0148] Step S7302: If the detection result is the first result, directly obtain the cache resource corresponding to the network request from the browser cache;
[0149] Specifically, when the detection result is the first result, that is, the detection result is that the network resource corresponding to the network request for the same resource exists in the browser cache, the proxy tool directly returns the network resource corresponding to the network request for the same resource from the browser cache to the browser, and the browser parses the network resource and displays it on the browser page. For example, when the network resource corresponding to the network request for the same resource is a picture, the picture is directly obtained from the browser cache and displayed on the corresponding browser page.
[0150] In the embodiment of the present application, by caching network resources in the browser cache, the user's network request can be responded to quickly.
[0151] Step S7303: if the detection result is the second result, re-obtaining the network resource corresponding to the network request from the server;
[0152] Specifically, when the detection result is the second result, that is, the detection result is that the network resource corresponding to the network request for the same resource does not exist in the browser cache, the request information of the network request for the same resource is modified, such as request parameters and request headers, and the proxy tool re-initiates the network request with the modified request information to the local server. The proxy tool obtains the resource corresponding to the network request from the local server, caches it in the browser cache, and finally returns it to the browser.
[0153] In an embodiment of the present application, when the network condition is normal, the user initiates a network request for the same resource. Optionally, the proxy tool detects whether there is a network resource corresponding to the network request for the same resource in the browser cache, and directly returns the network resource to the browser. It can also skip detecting the browser cache and choose to directly modify the request information of the network request for the same resource, and re-obtain the corresponding network resource from the local server. For example, the browser initiates a network request to the CDN server, and the proxy tool actively intercepts the network request. At this time, the proxy tool can return the network resource corresponding to the network request in the browser cache, or it can initiate a network request to the CDN server by modifying the request information of the network request.
[0154] In an embodiment of the present application, a method of returning the network resource corresponding to the network request is selected based on the speed of the network response. For example, when the network resource corresponding to the network request exists in the browser cache, the response speed of directly returning the cached resource from the browser cache is about 20ms, while returning the network resource corresponding to the network request from the local server requires about 20ms-5s. At this time, it will be faster to choose to return the cached resource from the browser cache.
[0155] It is understandable that the process from source code to user access is as follows:
[0156] (1) Compile the source code;
[0157] Specifically, compile the source code in the front-end file in the file corresponding to the source code, convert the source code into an abstract syntax tree, find the address of the network resource corresponding to the value of the node in the abstract syntax tree, download the network resource according to the network address and write the network resource into the project directory, for example: write into the assets directory. It is understandable that for a section of code (high-level language), the browser cannot directly understand it, so it must first be compiled into a language that the browser can understand. In addition, during the compilation process, by modifying the abstract syntax tree after the source code conversion, the source code can be further modified, compressed, and obfuscated.
[0158] (2) Write the compiled and modified source code into the file corresponding to the source code in the project, update the project, and deploy the project to the local server;
[0159] (3) The user accesses the browser page;
[0160] (4) The local server returns the HTML file corresponding to the page opened by the browser;
[0161] (5) The browser parses the HTML file and finds the network address corresponding to the network resource;
[0162] (6) The browser initiates a network request to an external server;
[0163] (7) The proxy tool processes the network request and returns the network resources corresponding to the network request;
[0164] Specifically, the proxy tool intercepts the browser's network request. If it checks that there is no cached resource corresponding to the network resource corresponding to the network request in the browser cache, it will request the network resource from the external server again, then cache the network resource in the browser cache, and return the network resource to the browser, which can be displayed to the user.
[0165] Specifically, when the browser initiates a network request to the same resource again, the proxy tool will still intercept the network request. At this time, if it detects that the network resource corresponding to the network request exists in the browser cache, it will return the cached network resource in the browser cache.
[0166] (8) The browser parses the network resources obtained from the browser cache.
[0167] Specifically, if the network resource is a picture, the picture is displayed in the browser so that the user can see the picture. In the embodiment of the present application, the browser caches the network resources, which can quickly respond to the user's network request and meet the user's needs. In addition, when the network is good, the network resources can be updated in time by initiating a new network request to the server, thereby improving the user's experience.
[0168] Step S734: if the cache time is not within the threshold range, re-obtain the network resource corresponding to the network request from the server;
[0169] Specifically, when the network condition is normal, when the cache time of the network resource corresponding to the network request is not within the set cache validity period, that is, the time interval between the user's second network request for the same resource and the first network request is not within the cache validity period, the proxy tool re-initiates a network request to the server, downloads the network resource corresponding to the network request on the server, and saves it to the project directory. The proxy tool caches the corresponding network resource in the project directory to the browser cache, and returns the cached resource to the corresponding page of the browser after obtaining the cached resource from the browser cache.
[0170] Step S704: if the network condition is abnormal, and the cache resource corresponding to the network request exists in the browser cache, directly obtain the cache resource corresponding to the network request;
[0171] Specifically, when the network condition is abnormal, that is, the network is disconnected or weak, access to local server resources will fail or take a long time. Based on the network request initiated by the user, it is detected whether there is a network resource corresponding to the network request in the browser cache. If so, the network resource corresponding to the network request is directly returned through the browser cache. Otherwise, the user cannot initiate a network request to the local server, and the request for the network resource corresponding to the network request fails.
[0172] In an embodiment of the present application, the network resources corresponding to the network request are saved in the browser cache. In the case of a network outage or weak network, the network resources corresponding to the user's network request can still be obtained, and the user's request can be responded to quickly.
[0173] See also Fig.10 , Fig.10It is a structural diagram of a network resource localization system provided in an embodiment of the present application;
[0174] like Fig.10 As shown, the network resource localization system 1000 includes: an electronic device 10, a local server 20 and an external server 30, wherein the electronic device 10 is communicatively connected to the local server 20, the local server 20 is communicatively connected to the external server 30, and the electronic device 10 can also be communicatively connected to the external server 30.
[0175] Specifically, the electronic device 10 is used to obtain files corresponding to the source code, and the files corresponding to the source code include front-end files and text files; if the front-end file meets the specified conditions, the corresponding network resources in the front-end file are identified; through the text file, the network resources are downloaded from the server, and the network resources are written to a preset project directory. After receiving the network request, the network resources corresponding to the network request are obtained from the project directory according to the network request.
[0176] Specifically, the local server 20 is used to download the corresponding network resources in the file corresponding to the source code from the external server 30, write the downloaded network resources into a preset project directory, and cache the network resources into the browser cache.
[0177] Specifically, the external server 30 is used to provide the local server with network resources corresponding to the network request.
[0178] In an embodiment of the present application, by saving the network resources in the external server in the local server, and then saving the network resources in the local server in the browser cache, the network resources corresponding to the network request can still be obtained from the browser cache in the case of weak network or network disconnection.
[0179] See also Fig.11 , Fig.11 It is a timing diagram of obtaining network resources corresponding to a network request provided by this application;
[0180] like Fig.11 As shown, the timing of obtaining the network resources corresponding to the network request includes:
[0181] Step S1101: Send a network request;
[0182] Specifically, the browser sends a network request to the local server, and the network request is generated by the user operating the browser interface.
[0183] Step S1102: Send a network request;
[0184] Specifically, the proxy tool actively intercepts the network request sent by the user in the browser and forwards the network request sent by the user to the local server.
[0185] Step S1103: Send a network request;
[0186] Specifically, the local server receives the network request forwarded by the proxy tool, and then forwards the network request to the external server.
[0187] Step S1104: Return the network resource corresponding to the network request;
[0188] Specifically, after receiving the network request, the external server responds to the network request and returns the network resources corresponding to the network request to the local server.
[0189] Step S1105: Return the network resource corresponding to the network request;
[0190] Specifically, the local server receives the network resource corresponding to the network request returned by the external server, and returns the network resource corresponding to the network request to the proxy tool.
[0191] Step S1106: Cache the network resource into the browser cache;
[0192] Specifically, after receiving the network resource corresponding to the network request returned by the local server, the proxy tool caches the network resource corresponding to the network request in the browser cache.
[0193] Step S1107: Return cache resources;
[0194] Specifically, the proxy tool caches the network resources corresponding to the network request in the browser cache, and then returns the cached resources in the browser to the browser.
[0195] Step S1108: Send a network request for the same resource;
[0196] Specifically, after a period of time, the browser sends a network request for the same resource to the server.
[0197] Step S1109: returning cache resources;
[0198] Specifically, after the proxy tool intercepts the network request for the same resource sent by the user, it returns the cached resource in the browser cache to the browser without initiating a network request to the server.
[0199] In the embodiment of the present application, by caching the network resources corresponding to the network request in the browser cache, the user request can be responded to quickly to meet the user's needs.
[0200] See also Fig.12 , Fig.12 It is a structural diagram of a network resource localization device provided in an embodiment of the present application;
[0201] like Fig.12 As shown, the network resource localization device 1200 includes:
[0202] A file acquisition module 121, used to acquire files corresponding to the source code, where the files corresponding to the source code include front-end files and text files;
[0203] A network resource determination module 122, configured to identify a corresponding network resource in the front-end file if the front-end file meets a limiting condition;
[0204] The network resource download module 123 is used to download network resources from the server through a text file and write the network resources into a preset project directory;
[0205] The network resource acquisition module 124 is used to acquire the network resources corresponding to the network request from the project directory according to the network request after receiving the network request.
[0206] In an embodiment of the present application, the localization device of network resources can be a software module, which includes a number of instructions stored in a memory. The processor can access the memory and call the instructions for execution to complete the localization method of network resources in the above-mentioned embodiments.
[0207] In the embodiments of the present application, the localization device of network resources can also be constructed by hardware devices. For example, the localization device of network resources can be constructed by one or more chips, and each chip can work in coordination with each other to complete the localization method of network resources described in the above embodiments. For another example, the localization device of network resources can also be constructed by various logic devices, such as general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field programmable gate arrays (FPGAs), single-chip microcomputers, ARM (AcornRISC Machine) or other programmable logic devices, discrete gates or transistor logic, discrete hardware components, or any combination of these components.
[0208] The localization device of network resources in the embodiment of the present application can be a device, or a component, integrated circuit, or chip in a terminal. The device can be a mobile electronic device or a non-mobile electronic device. Exemplarily, the mobile electronic device can be a mobile phone, a tablet computer, a laptop computer, a palmtop computer, an in-vehicle electronic device, a wearable device, an ultra-mobile personal computer (ultra-mobile personal computer, UMPC), a netbook or a personal digital assistant (personal digital assistant, PDA), etc., and the non-mobile electronic device can be a server, a network attached storage (Network Attached Storage, NAS), a personal computer (personal computer, PC), a television (television, TV), a teller machine or a self-service machine, etc., which is not specifically limited in the embodiment of the present application.
[0209] The localization device of the network resources in the embodiment of the present application may be a device having an operating system. The operating system may be an Android operating system, an iOS operating system, or other possible operating systems, which are not specifically limited in the embodiment of the present application.
[0210] The network resource localization device provided in the embodiment of the present application can achieve Figure 3 To avoid repetition, the various processes implemented by the method embodiment are not described here.
[0211] It should be noted that the above device can execute the localization method of network resources provided in the embodiment of the present application, and has the corresponding functional modules and beneficial effects of the execution method. For technical details not fully described in the device embodiment, please refer to the localization method of network resources provided in the embodiment of the present application.
[0212] In an embodiment of the present application, a localization device for network resources is provided, wherein a file acquisition module 121 is used to acquire files corresponding to source code, and the files corresponding to source code include front-end files and text files; a network resource determination module 122 is used to identify the corresponding network resources in the front-end file if the front-end file meets the specified conditions; a network resource download module 123 is used to download network resources from a server through a text file and write the network resources to a preset project directory; a network resource acquisition module 124 is used to acquire the network resources corresponding to the network request from the project directory according to the network request after receiving the network request.
[0213] By caching network resources in the browser cache, this application can still respond to users quickly in the event of a network outage or weak network, without affecting user access to resources.
[0214] Please refer to Fig.13 , Fig.13 It is a structural schematic diagram of an electronic device provided in an embodiment of the present application;
[0215] like Fig.13 As shown, the electronic device 10 includes one or more processors 101 and a memory 102. Fig.13 A processor 101 is taken as an example.
[0216] The processor 101 and the memory 102 may be connected via a bus or other means. Fig.13 The example of connecting through bus is taken in the following.
[0217] A processor, configured to execute the method for localizing network resources in any embodiment of the present application, comprising:
[0218] Get the file corresponding to the source code, which includes the front-end file and the text file. If the front-end file meets the preset conditions, identify the corresponding network resources in the front-end file, download the corresponding network resources in the front-end file from the server, write the network resources to the preset project directory, and after receiving the network request, get the network resources corresponding to the network request from the project directory according to the network request.
[0219] This application caches network resources in the browser cache, so that in the case of network disconnection or weak network, it can still respond to users quickly without affecting user access to resources.
[0220] The memory 102, as a non-volatile computer-readable storage medium, can be used to store non-volatile software programs, non-volatile computer executable programs and modules, such as program instructions / modules corresponding to the method for localizing network resources in the embodiment of the present invention. The processor 101 executes various functional applications and data processing of the electronic device by running the non-volatile software programs, instructions and modules stored in the memory 102, that is, the method for localizing network resources in the above method embodiment is implemented.
[0221] The memory 102 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other non-volatile solid-state storage device. In some embodiments, the memory 102 may optionally include a memory remotely arranged relative to the processor 101. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0222] One or more modules are stored in the memory 102, and when executed by one or more processors 101, the localization method of network resources in any of the above method embodiments is executed, for example, the above described method is executed. Figure 3 The steps shown.
[0223] An embodiment of the present application also provides a computer program product, which includes one or more program codes, and the program code is stored in a non-volatile computer-readable storage medium. A processor of an electronic device reads the program code from the non-volatile computer-readable storage medium, and the processor executes the program code to complete the steps of the network resource localization method provided in the above embodiment.
[0224] Through the description of the above implementation methods, ordinary technicians in this field can understand that all or part of the steps of implementing the above embodiments can be completed by hardware, or by hardware related to program code, and the program can be stored in a non-volatile computer-readable storage medium. The non-volatile computer-readable storage medium mentioned above can be a read-only memory, a disk or an optical disk, etc.
[0225] Through the description of the above implementation methods, a person of ordinary skill in the art can clearly understand that each implementation method can be implemented by means of software plus a general hardware platform, and of course, can also be implemented by hardware. A person of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment method can be completed by instructing the relevant hardware through a computer program, and the program can be stored in a non-volatile computer-readable storage medium. When the program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, the non-volatile computer-readable storage medium can be a disk, an optical disk, a read-only memory (ROM) or a random access memory (RAM), etc.
[0226] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Under the concept of the present application, the technical features in the above embodiments or different embodiments can also be combined, the steps can be implemented in any order, and there are many other changes in different aspects of the present application as mentioned above, which are not provided in detail for the sake of simplicity. Although the present application has been described in detail with reference to the aforementioned embodiments, a person of ordinary skill in the art should understand that the technical solutions described in the aforementioned embodiments can still be modified, or some of the technical features can be replaced by equivalents. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the scope of the technical solutions of the embodiments of the present application.
Claims
1. A method for localizing network resources, characterized in that: Applied to electronic equipment, the method comprises: Obtain files corresponding to the source code, wherein the files corresponding to the source code include front-end files and text files; If the front-end file meets the limiting conditions, identifying the corresponding network resource in the front-end file; Download the network resource from the server through the text file, and write the network resource into a preset project directory; After receiving the network request, the network resource corresponding to the network request is obtained from the project directory according to the network request.
2. The method according to claim 1, characterized in that Before obtaining the file corresponding to the source code, the method further includes: Creating a project, the project including files corresponding to the source code and the preset project directory, the preset project directory being used to store the network resources corresponding to the network request; The file name and / or file path of the file corresponding to the source code is limited to obtain the limited condition, wherein the limited condition includes the limited file name and / or the limited file path.
3. The method according to claim 2, characterized in that Identify the corresponding network resources in the front-end file, including: Convert the source code into an abstract syntax tree, where the abstract syntax tree includes a plurality of nodes, where the nodes store a string literal of a first resource address, where the first resource address is a network resource address; According to the network resource address, the corresponding network resource in the front-end file is identified.
4. The method according to claim 3, characterized in that After writing the network resource into the preset project directory, the method further includes: Marking the address of the network resource written into the preset project directory as a second resource address, where the second resource address corresponds one-to-one with the first resource address; Modify the first resource address corresponding to the string literal of the node of the abstract syntax tree to the second resource address, to obtain a modified abstract syntax tree; The modified abstract syntax tree is converted into a modified source code, and the modified source code is written into a file corresponding to the source code, the project is updated to obtain an updated project, and the updated project is deployed.
5. The method according to claim 4, characterized in that The electronic device includes a browser, the browser includes a browser cache, and after receiving the network request, obtaining the network resource corresponding to the network request from the project directory according to the network request, including: According to the network request, cache the network resources corresponding to the network request in the project directory into the browser cache to obtain the cached resources; The cache resource is obtained from the browser cache.
6. The method according to claim 5, characterized in that The method further comprises: After receiving a network request for the same resource, obtaining the network resource corresponding to the network request according to a cache strategy, wherein the cache strategy includes a cache time and a network status, wherein the network status includes a normal status and an abnormal status; If the network condition is normal, obtaining the network resource corresponding to the network request according to the cache time; If the network condition is abnormal, and the cache resource corresponding to the network request exists in the browser cache, the cache resource corresponding to the network request is directly obtained.
7. The method according to claim 6, characterized in that Obtaining the network resource corresponding to the network request according to the cache time, including: If the cache time is within the threshold range, searching whether the network resource corresponding to the network request exists in the browser cache, obtaining a detection result, and acquiring the network resource corresponding to the network request according to the detection result; If the cache time is not within the threshold range, the network resource corresponding to the network request is obtained from the server again.
8. The method according to claim 7, characterized in that The detection result includes a first result and a second result. According to the detection result, obtaining a network resource corresponding to the network request includes: If the detection result is the first result, directly obtaining the cache resource corresponding to the network request from the browser cache, and the first result is that the network resource corresponding to the network request exists in the browser cache; If the detection result is the second result, the network resource corresponding to the network request is obtained from the server again, and the second result is that the network resource corresponding to the network request does not exist in the browser cache.
9. An electronic device, characterized in that: include: At least one processor and at least one memory, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor executes the steps of the method according to any one of claims 1 to 8.
10. A network resource localization system, characterized in that: The system comprises: The electronic device as claimed in claim 9; The local server is used to download the corresponding network resources in the front-end file from the external server, write the downloaded network resources into a preset project directory, and cache the network resources into the browser cache; The external server is used to provide the local server with network resources corresponding to the network request.