Implemented Next.js performance optimization #250
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR implements several performance optimizations to enhance the application's efficiency and Lighthouse scores. The changes focus on form component rendering optimization, lazy loading, and memoization of frequently used components.
Key improvements:
Related Issue
Addresses performance optimization requirements for form components and tool request handling.
Type of Change
Please select the type(s) of change that apply and delete those that do not.
-[x] Performance improvement: A change that improves performance.
-[x] Code style update: Changes that do not affect the meaning of the code (e.g., formatting).
-[x] Refactoring: A code change that neither fixes a bug nor adds a feature.
Proposed Solution
The solution implements several performance optimizations:
1.Form Component Memoization:
const PrimaryTextFieldInput = forwardRef((props, ref) => {
const {
id,
error,
placeholder,
title,
helperText,
isDescription,
description,
borderColor,
extraInputProps,
extraInputLabelProps,
...otherProps
} = props;
const TextFieldElementConfig = {
id,
label: title,
fullWidth: true,
helperText,
InputLabelProps: styles.inputLabelProps(error, extraInputLabelProps),
InputProps: styles.inputProps(error, extraInputProps),
FormHelperTextProps: styles.helperTextProps(isDescription, error),
autoComplete: 'off',
placeholder,
};
return (
<TextFieldElement
inputRef={ref}
{...TextFieldElementConfig}
{...otherProps}
/>
);
});
2.Optimized File Upload Handling:
const PrimaryFileUpload = forwardRef((props, ref) => {
const {
id,
name,
placeholder,
title,
color,
bgColor,
control,
setValue,
multiple,
error,
} = props;
const fileInputRef = useRef();
const [files, setFiles] = useState([]);
3.Enhanced Form State Management:
const ToolRequestForm = (props) => {
const { id, inputs } = props;
const theme = useTheme();
const dispatch = useDispatch();
const { handleOpenSnackBar } = useContext(AuthContext);
const { communicatorLoading } = useSelector((state) => state.tools);
const { data: userData } = useSelector((state) => state.user);
// Extract default values from inputs
const defaultValues = inputs.reduce((acc, input) => {
if (input.defaultValue !== undefined) {
acc[input.name] = input.defaultValue;
}
return acc;
}, {});
const { register, control, handleSubmit, setValue, errors } = useForm({
defaultValues,
});
const watchedValues = useWatch({ control });
4.Efficient Input Rendering:
const PrimarySelectorInput = forwardRef((props, ref) => {
const {
id,
label,
placeholder,
error,
control,
helperText,
displayEmpty,
menuList,
bgColor,
color,
extraInputProps,
...otherProps
} = props;
const renderPlaceholder = (selected) => {
if (selected?.length === 0)
return (
<Typography {...styles.placeholderProps}>{placeholder}
);
};
const SelectMenuConfig = {
id,
name: id,
control,
inputRef: ref,
label,
helperText,
options: menuList,
InputLabelProps: styles.inputLabelProps,
FormHelperTextProps: styles.helperTextProps(error),
...styles.selectInputProps(
ExpandMoreIcon,
bgColor,
color,
renderPlaceholder,
displayEmpty,
extraInputProps
),
};
return <SelectElement {...SelectMenuConfig} {...otherProps} />;
});
How to Test
Monitor performance metrics:
Use React DevTools Profiler to measure render times
Check Lighthouse scores before and after changes
Verify memory usage in Chrome DevTools
Test form interactions:
Verify smooth form field updates
Check file upload performance
Test multiple concurrent form submissions
Unit Tests
Documentation Updates
Indicate whether documentation needs to be updated due to this PR.
If yes, describe what documentation updates are needed and link to the relevant documentation.
Checklist
Additional Information
The performance improvements focus on optimizing the form handling components, which are critical for user interaction. The changes maintain functionality while significantly reducing unnecessary renders and improving overall application performance.