Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

umi@4没有modifyClientRenderOpts了?用什么替代 #9761

Closed
qiujie8092916 opened this issue Nov 13, 2022 · 11 comments · Fixed by #9787, #9765 or #9871
Closed

umi@4没有modifyClientRenderOpts了?用什么替代 #9761

qiujie8092916 opened this issue Nov 13, 2022 · 11 comments · Fixed by #9787, #9765 or #9871

Comments

@qiujie8092916
Copy link

qiujie8092916 commented Nov 13, 2022

What happens?

umi@3下会在运行时插件里使用modifyClientRenderOpts方法,umi@4下会报 Error: register failed, invalid key modifyClientRenderOpts from

Mini Showcase Repository(REQUIRED)

How To Reproduce

Steps to reproduce the behavior:

  1. 插件里使用api.addRuntimePlugin添加 js
  2. js 里调用modifyClientRenderOpts报错 Error: register failed, invalid key modifyClientRenderOpts from

Expected behavior

  1. 替代方式

Context

  • Umi Version: 4.0.30
  • Node Version:
  • Platform:
@github-actions
Copy link

由于缺乏足够的信息,我们暂时关闭了该 Issue。请修改(不要回复) Issue 提供最小重现以重新开启。谢谢。

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 13, 2022
@fz6m
Copy link
Member

fz6m commented Nov 14, 2022

api.modifyContextOpts ,可参考 .umi/umi.ts 中的参数列表。

@qiujie8092916
Copy link
Author

api.modifyContextOpts ,可参考 .umi/umi.ts 中的参数列表。

api.modifyContextOpts 在哪里调用?

@qiujie8092916
Copy link
Author

api.modifyContextOpts ,可参考 .umi/umi.ts 中的参数列表。

之前在modifyClientRenderOpts 里用到了参数historyrootElement,并在运行时修改了history
参照.umi/umi.ts里的modifyContextOpts参数是{}呢?

@fz6m
Copy link
Member

fz6m commented Nov 16, 2022

// src/app.ts

export const modifyContextOpts = () => {
  return {
    // 能 overrides 的参数,可参考 .umi/umi.ts 传参
    basename: '/cc',
    rootElement: document.getElementById('#sub-root')
  }
}

@qiujie8092916
Copy link
Author

不好意思,可能之前没有表达清楚
我们是这样的:

// umi@3
export const modifyContextOpts => (memo) {
     const history = memo.history;
     const rootElement = memo.rootElement;

    // overwrite history
    history.go = function() { 
        // use rootElement
        // do sth
    }

    history.goBack = function() { 
        // use rootElement
        // do sth
    }

    history.replace = function() { 
        // use rootElement
        // do sth
    }
    
    return {
        ...memo,
        history
    }
}

请问在umi@4下怎么解

@fz6m
Copy link
Member

fz6m commented Nov 16, 2022

rootElement 的位置在 modifyContextOpts 中可以修改。

history 可以在这里加 patch ,但是有一些问题,严格来说他的执行时机可能并不在所有 history 运用之前。

// src/app.ts

export const rootContainer = (
  rootContainer: React.ReactNode,
  args: {
    history: any
  }
) => {
  console.log('rootContainer: ', rootContainer)
  console.log('args: ', args)

  const history = args.history
  const originPush = history.push
  history.push = (path: any, state: any) => {
    originPush(path, state)
  }
  return rootContainer
}

目前没有非常好的方案,可以提 feature 支持 context 的修改。

@fz6m
Copy link
Member

fz6m commented Nov 17, 2022

继上面一条消息,新版本发布后( >=4.0.31 )。

将可以在 render 前获取到 history ,从而实现你的 patch 需求。

import { history } from 'umi'

export const render = (oldRender: any) => {
  // use history
  oldRender()
}

@fz6m
Copy link
Member

fz6m commented Nov 17, 2022

部分 qiankun 应用有问题,改动被回滚了。

楼上的方案只能在 4.0.31 用(确保你没有使用 qiankun 插件),在 4.0.32 不可用,有需求的话,等待新的改动方案吧。

@qiujie8092916
Copy link
Author

继上面一条消息,新版本发布后( >=4.0.31 )。

将可以在 render 前获取到 history ,从而实现你的 patch 需求。

import { history } from 'umi'

export const render = (oldRender: any) => {
  // use history
  oldRender()
}

此方案不能满足,我们在重写history的方法里,需要用到memo.rootElement

@fz6m
Copy link
Member

fz6m commented Nov 24, 2022

今天发版后可调用 modifyClientRenderOpts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants