Thoughts on CSS. JS file publishing mechanism

Source: Internet
Author: User

Liehuo. in the development process, sometimes the page cannot be updated in time due to cache problems, sometimes the page introduces unnecessary style script files, sometimes due to too many files, too large bytes lead to slow page performance. To solve these problems, I have come up with a preliminary solution.

The solution is as follows:

 

Description

Remarks

Resource level

    Page resource level:

  1. Global (Common)
  2. Module Level)
  3. Page)

 

Optimization Scheme

    The optimization mainly involves the following aspects:

  1. Reduce the size of a single file in bytes
  2. Reduce the number of files (that is, reduce the number of http requests)

     

Other optimizations are based on development skills, depending on the professional level.

    File output Scheme

  1. Merge
  2. Compression

    The following four schemes are developed based on the resource level and optimization scheme:

    1. Directly reference a single file and import it in sequence (<link/> and <script/>)

    2. Single file Compression Based on 1

    3. Merge data into a single file at the resource level

    4. Based on 3, compress the merged files



Thoughts

Solution 3: how to determine the number of merged files?


Cache Problems

Timestamp suffix

 

F2econfig_json

Configuration file:

Var f2eJson = {
"Version": "v3", // Version

"Update": "20091015", // Update the cache based on the timestamp

"Compress": "1", // file output Scheme (1, 2, 3, 4)

"Common": [// public style of the whole site

"Common/global.css"

],

"Module": {// Module and Style

"Module 1": ["product,.css"]

},

"Page": {// the style of each Page

"Index. php": ["", "sys/index.css"],

// The reference name of the first element storage module. leave it blank if none exist.

"Search. php": ["Module 1", "product/search.css"]

// Reference multiple modules separated by commas (,), for example, Module 1 and module 2.

}

}

  1. Php has a dedicated json conversion Library
  2. The Configuration Structure of js is the same as that of css configuration json.

F2engine. php

1. Read the json configuration file and convert it to a php array.
2. Search for style resources on the page based on the resource parameters of each page.
3. process according to the file output Scheme
4. output on the page
  1. If the configuration file is not updated, json is parsed as an array only once.
  2. Parse the resource only once and save the result for direct use next time

     

Example

    For example, the style reference of the search. php page

     

  1. Locate the Page-level resource based on the Page: Page ["search. php"]
  2. Search for the referenced module style based on the first value of the array
  3. Determine the file output scheme based on the "Compress" value in the configuration file and process it
  4. Output on the page

    // Full site style common

    <Link type = "text/css" href = "common/global.pack.css? 20091015 "rel =" stylesheet "/>

    // Module style module

    <Link type = "text/css" href = "product.pack.css? 20091015 "rel =" stylesheet "/>

    // Page Style

    <Link type = "text/css" href = "product/serach.pack.css? 20091015 "rel =" stylesheet "/>

     

    Note: after compression, the file name becomes search.pack.css.

     

 

 

The Code is under development. You are welcome to discuss it with us.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.