preface

The volume of the main package of wechat small program cannot exceed 2M, so it is necessary to strictly control which pages are placed in the main package and which pages are placed in the subpackage. After all, when the business is complex, small programs become “big programs,” and the main package is out of size, there is no way to submit code.

In projects, sharing support pages are sometimes placed in the main package due to poor initial thinking. If they are moved from the main package to subcontracting at this time, or because the previous subcontracting rules are not reasonable, they need to be adjusted. The file path is changed. The page cannot be found in the original share path.

The solution

In the main entryPage package plus a unified portal page, all the pages of the support to share share the path of the path is/pages/entryPage/entryPage?” ActionID =xx& extra parameters “, then concatenate the page itself needs to bring parameters. ActionID is the unique identifier of the page, such as 100001 for the home page and 100002 for the personal center page. Once this ID is fixed, it cannot be fixed (i.e. redefined).

Enter the share link “/ pages/entryPage/entryPage? ActionID =xx& additional parameter “, that is, enter the entryPage page, at this time, entryPage obtains the actionID, and then use the actionID to match the page for forwarding, and redirect to the specific shared page.

risk

Before implementing this program, several points need to be made clear:

  1. For old cards and small programs that have been shared out, there will be “pain” of not finding the page. Require users to share, share out link is “/ pages/entryPage/entryPage? ActionID =xx& extra parameter”

  2. It is necessary to confirm in advance with the product and other parties whether the page path can be changed, especially the communication between the configuration side, operation side, back end, hardware side and other parties. If it is printing small program code, that is, the code is printed on the hardware and cannot be replaced in the short term, try not to change the page path. All of these require multi-party communication to avoid problems.

earnings

The plan, can put some unnecessary main package page removed, although there will be pain (the risk point), but in the long term, which can reduce the package size, 2 it is even if the subsequent support sharing page to change the package path (for example, from the subcontract to the subcontract B), also does not affect the outside world, this time is just inside the front technology optimization. External oriented Shared links is always “/ pages/entryPage/entryPage? ActionID =xx& extra parameter”

code

App. json main package added entryPage page:

{
    "pages": [
        "pages/entryPage/entryPage"],}Copy the code

Entrypage.js key code

Page({
    /** * handle actionObj, implement redirect *@param {*} action* /
    handleAction(options) {
        if(! options)throw 'options is invalid ';
        
        // It is recommended to extract the configuration table separately
        let path = ' ';
        switch (options.actionID) {
            case 100001: {
                path = 'Product page path';
                break;
            }
            case 100002: {
                path = 'Personal page path';
                break;
            }
            case 100003: {
                path = 'Set page path';
                break;
            }
            default: {
                path = 'Home path'; }}// Assemble parameters other than actionID
        let params = ' ';
        for (let key in options) {
            if(key ! = ='actionID') {
                params += ` &${key}=${options[key]}`; }}// Redirect to the actual landing page
        wx.redirectTo({ url: `${path}?${params.slice(1)}` });
    },
    
    async onLoad(options) {
        try {
            this.handleAction(options);
        } catch (err) {
            console.warn('err ==>', err);
            // Failed to parse options
            wx.switchTab({
                url: '/pages/home/home'}); }}});Copy the code